On Sunday, 3 November 2019 at 16:55:36 UTC, Vinod K Chandran wrote:
Hi all,
I can do this in C++.
#include <iostream>
using namespace std ;

#define end };
#define log(x)  cout << x << endl
#define wait std::cin.get()


int main() {
log("Trying to avoid the visual clutter aused by closing curly braces") ;
    string myStr = "Now, code looks more elegant" ;
    log(myStr) ;
    wait ;
end
How can i do this in D ? Especially the " #define end }; ". I've tried " alias end = } " but didn't worked. Edit : How can add syntax highlighting and coloring in code posted on this comment ?

If this is about logging functionality, check std.experimental.log package, it contains all necessary logging functionality.

About macros there aren't any similar to preprocessor macros in c/c++, however you can replace them with three options depending on your needs:

1. Just a simple function in conjunction eith ctfe: https://tour.dlang.org/tour/en/gems/compile-time-function-evaluation-ctfe
2. string mixins: https://dlang.org/articles/mixin.html
3. template mixins: https://dlang.org/spec/template-mixin.html

I'd say number 2 should be suitable for your example given you'd like to inject statements into body of some function.

Best regards,
Alexandru.

Reply via email to