8 ms·
The D Programming language has a neat trick to block commenting out code for debugging purposes. Wrapping code in a version(none) {} block will exclude the bloc
by chhs 9y ago
The D Programming language has a neat trick to block commenting out code for debugging purposes. Wrapping code in a version(none) {} block will exclude the block from compilation. The code inside the block must still be syntactically valid so it avoids the problem mentioned in the linked post.
The D Programming Language - Conditional Compilation: https://dlang.org/spec/version.html https://dlang.org/spec/version.html
- TeMPOraL 9y agoDitto for Common Lisp, where conditional compilation (with #+ and #- reader macros) is widely used to comment out s-expressions. EDIT: I only now noticed it was mentioned in the article. I'll therefore add that the example from the article, of using #+nil, is actually not recommended, because it's technically possible that somebody adds a feature flag called "nil". Therefore, it's preferred to use #+(or) or #-(and) for comments (they're degenerate forms of conditional compilation expressions like #+(or flag1 flag2 flag3)).
- msoucy 9y agoD also supports nesting block comments with /+ +/, which helps when you have example code in your documentation
- LeifCarrotson 9y agoIn what ways is that better than "if(false) { }"?
- wruza 9y agoFrom linked examples it is clear that version() is allowed outside of functions. Since version() already serves a feature, there is no need to overload the meaning of if().
- aardvark179 9y agoIt's commonly done in C with a #if 0 / #endif preprocessor macro.