Is this a new bug ?

Adam D. Ruppe destructionator at gmail.com
Sat Jul 20 03:58:36 UTC 2019


On Saturday, 20 July 2019 at 03:31:41 UTC, Newbie2019 wrote:
>
> template Test(alias T){
>         pragma(msg, T.stringof);

Believe it or not, but this can result in the function being 
called. This is D's optional parenthesis feature, to do simple 
property syntax.

Not a bug.

> I want get the function name "t2", "t3" for string mixin,  how 
> do I do this in D ?

You should never actually need that! What is your use case?

In most cases I see people doing .stringof, the correct solution 
is to simply... not. Use the local name directly.

If you do need the name - and the only cases for this ought to be 
declaring a new item for public consumption or for interfacing 
with users - the correct way to get an identifier is 
`__traits(identifier, T)`... but again, more often than not, you 
do NOT need this and it is wrong to try!

.stringof should almost never be used except for debugging, and 
string concatenation in string mixings is actually only necessary 
for new declaration names.

OK:

mixin("int " ~ name ~ " = T;");

Wrong:

mixin("int " ~ name ~ " = " ~ T.stringof ~ ";");


More information about the Digitalmars-d-learn mailing list