www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using alias parameters with class members in CTFE (related to UDAs)

reply Johannes Pfau <nospam example.com> writes:
Does anyone know why this code is not working?
http://dpaste.dzfl.pl/b89e7b3f

It seems calling "enum a = __traits(getAttributes, Class.member);" is OK
without an instance. But if I wrap the __traits call into another
template with alias parameter like this:

-----------
auto getAttribute(alias target)()
{
    return __traits(getAttributes, target)[0];
}
-----------

I can no longer call it at compile time ("enum b =
getAttribute!(Class.member)").


What I want to do ultimately is assigning special values to all class
members which have a specific UDA while avoiding runtime overhead as
much as possible. In pseudo code:

------------
instance i1;

static foreach member in i1
    static if i1.hasUDA(UdaType)
        member = specialValue();
------------

Should compile down to only this:
instance i1;

i1.member1 =  specialValue();
i1.member2 =  specialValue();
i1.member3 =  specialValue();
Jul 23 2013
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 7/23/13, Johannes Pfau <nospam example.com> wrote:
 Does anyone know why this code is not working?
 http://dpaste.dzfl.pl/b89e7b3f
Add 'static' to getAttribute and it will work. I know, it's weird, and I've seen this sort of workaround used before. I think it's a compiler bug.
Jul 23 2013
parent Johannes Pfau <nospam example.com> writes:
Am Wed, 24 Jul 2013 02:13:50 +0200
schrieb Andrej Mitrovic <andrej.mitrovich gmail.com>:

 On 7/23/13, Johannes Pfau <nospam example.com> wrote:
 Does anyone know why this code is not working?
 http://dpaste.dzfl.pl/b89e7b3f
Add 'static' to getAttribute and it will work. I know, it's weird, and I've seen this sort of workaround used before. I think it's a compiler bug.
Thanks! I already feared I'd have to use some string mixin tricks :-)
Jul 23 2013