Weird behavior with UDAs

realhet real_het at hotmail.com
Sat Jun 13 12:55:36 UTC 2020


Hello, I have a problem I can't even understand with the code 
below:

https://run.dlang.io/is/7yXAEA

import std.stdio, std.range, std.algorithm, std.traits, std.meta;

struct UNIFORM{ string name; }

struct A{
     int i1;
     @UNIFORM() int i2;
     @UNIFORM("fuzz") int i3;
     @UNIFORM int i4;
}

template getUDA(alias a, U){
   static if(hasUDA!(a, U)) enum getUDA = getUDAs!(a, U)[$-1];
                       else enum getUDA = U.init;
}

pragma(msg, getUDA!(A.i1, UNIFORM)); //UNIFORM(null)
pragma(msg, getUDA!(A.i2, UNIFORM)); //UNIFORM(null)
pragma(msg, getUDA!(A.i3, UNIFORM)); //UNIFORM("fuzz");
pragma(msg, getUDA!(A.i4, UNIFORM)); //Error: initializer must be 
an expression, not UNIFORM

struct UNIFORM2{ string name=""; }
struct B{ @UNIFORM2() int i1; }

pragma(msg, getUDA!(B.i1, UNIFORM2)); //UNIFORM("") instead if 
UNIFORM(null)

//------------------------------------------------------------------------

My first question is, how to avoid that error with A.i4?  Why is 
there a difference between @UNIFORM and @UNIFORM(), do the first 
returns a type and the later returns a value?

My second quertion is, why the UNIFORM struct with uninitialized 
string producing UNIFORM(null).
How can be a difference when I say name="", and it's just the 
same as the default string initializer, and then it produce 
UNIFORM("")?

Thank You!



More information about the Digitalmars-d-learn mailing list