Feature discussion: __traits(getSource, function)

Nick Sabalausky a at a.a
Fri Oct 15 21:49:46 PDT 2010


"Robert Jacques" <sandford at jhu.edu> wrote in message 
news:op.vknih4dy26stm6 at sandford.myhome.westell.com...
>
> I would favor some form of AST as opposed to raw strings,

Definitely, but strings would probably a lot easier to put in and work well 
enough in the meantime.

I just came across another case where access to original source would be 
useful. I have a helper tool in my SemiTwistDTools lib that I use all the 
time that works like this:

int a=3;
mixin(traceVal!("a+4"));

// runtime output:
a+4: 7

Which is almost as much a pain to type as it is useful. But if there was a 
finer-graned version of __traits(getSource, function), say, to get the 
source of a parameter from the caller's side, then I could probably just 
simplify it to this:

traceVal(a+4);

By doing something like this:

void traceVal(T, string str = __traits(getParamSource, expr))(T expr)
{
    writeln(str, ": ", expr);
}

Which would be similar to this trick already in D2:

int bar(int line = __LINE__)()
{
 return line;
}

void main()
{
#line 1000
 writeln(bar()); // Output: 1000
}

...Although in testing that I just noticed that defining bar this way 
doesn't work the same:

template bar(int line = __LINE__)
{
 enum bar = to!string(line);
}

Bug?





More information about the Digitalmars-d mailing list