Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
December 17, 2020 Getting the source text of an expression | ||||
---|---|---|---|---|
| ||||
In C, you can use a macro to get the source text of an expression as a string. For example #include <stdio.h> #define PRINT_INT(x) printf(#x " = %d\n", x) int main(){ // prints "3 = 3" PRINT_INT(3); int x = 4; // prints "x = 4" PRINT_INT(x); #define FOO 5 // prints "FOO = 5" PRINT_INT(FOO); #define BAR FOO // prints "BAR = 5" PRINT_INT(BAR); return 0; } Is there a feature of D that allows you to achieve a similar goal? I’ve used this in the past for logging for example. |
December 17, 2020 Re: Getting the source text of an expression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dave P. | On Thursday, 17 December 2020 at 19:45:38 UTC, Dave P. wrote:
> In C, you can use a macro to get the source text of an expression as a string. For example
>
> #include <stdio.h>
> #define PRINT_INT(x) printf(#x " = %d\n", x)
>
> int main(){
> // prints "3 = 3"
> PRINT_INT(3);
> int x = 4;
> // prints "x = 4"
> PRINT_INT(x);
> #define FOO 5
> // prints "FOO = 5"
> PRINT_INT(FOO);
> #define BAR FOO
> // prints "BAR = 5"
> PRINT_INT(BAR);
> return 0;
> }
>
> Is there a feature of D that allows you to achieve a similar goal? I’ve used this in the past for logging for example.
Something like that?
import std.stdio;
void print_int(alias n)()
{
writeln(n.stringof~"=", n);
}
void main()
{
int x = 42;
print_int!(x);
print_int!(7);
}
|
December 17, 2020 Re: Getting the source text of an expression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dave P. | On Thursday, 17 December 2020 at 19:45:38 UTC, Dave P. wrote: > In C, you can use a macro to get the source text of an expression as a string. For example > > #include <stdio.h> > #define PRINT_INT(x) printf(#x " = %d\n", x) > > int main(){ > // prints "3 = 3" > PRINT_INT(3); > int x = 4; > // prints "x = 4" > PRINT_INT(x); > #define FOO 5 > // prints "FOO = 5" > PRINT_INT(FOO); > #define BAR FOO > // prints "BAR = 5" > PRINT_INT(BAR); > return 0; > } > > Is there a feature of D that allows you to achieve a similar goal? I’ve used this in the past for logging for example. https://code.dlang.org/packages/jdiutil |
December 17, 2020 Re: Getting the source text of an expression | ||||
---|---|---|---|---|
| ||||
Posted in reply to FreeSlave | On Thursday, 17 December 2020 at 21:24:40 UTC, FreeSlave wrote:
> On Thursday, 17 December 2020 at 19:45:38 UTC, Dave P. wrote:
>> [...]
>
> Something like that?
>
> import std.stdio;
>
> void print_int(alias n)()
> {
> writeln(n.stringof~"=", n);
> }
>
> void main()
> {
> int x = 42;
> print_int!(x);
> print_int!(7);
> }
Very cool! Where can I read about what an alias as a template parameter does?
|
December 18, 2020 Re: Getting the source text of an expression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dave P. | On Thursday, 17 December 2020 at 21:40:09 UTC, Dave P. wrote: > Very cool! Where can I read about what an alias as a template parameter does? https://dlang.org/spec/template.html#aliasparameters https://github.com/PhilippeSigaud/D-templates-tutorial |
Copyright © 1999-2021 by the D Language Foundation