Is it possible to obtain textual representation of an arbitary code?

Oleksii Skidan al.skidan at gmail.com
Fri Jan 26 11:18:21 UTC 2018


Hi,

I wonder if it's possible to convert D language code into a 
string at compile time? C/C++ preprocessor has this feature 
built-in: `#` preprocessing operator allows converting a macro 
argument into a string constant. See the following code snippet 
for example:

```cplusplus
#define ASSERT(EXPR) some_function((EXPR), #EXPR)

// Elsewhere in the code:
void some_function(bool const condition, char const* const expr) {
   if (!condition) {
      printf("Assertion failed for: %s\n", expr);
   }
}

// Usage:
ASSERT(a == b); // Will print "Assertion failed for: a == b"
```

I could imagine a mixin-based solution in D:
```d
// Usage:
ASSERT!"a == b";
```
But it seems a bit alien to me. First of all, it kind of 
stringly-typed one. Secondly, neither IDEs nor advanced text 
editors are able to figure out that the string contains actual D 
code, and so neither syntax highlighting nor code assistance work 
with this approach.

BR,
--
Oleksii




More information about the Digitalmars-d-learn mailing list