How to assign a delegate to a var ?

bioinfornatics via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 3 08:23:48 PDT 2014


delegate has a context pointer that it uses when executing
>> at run-time. However, there can't be a run-time context of a 
>> delegate that is created at compile-time. I think that is why 
>> the segfault.
>>
>> Is there a reason why it needs to be a delegate?
>>
>> Replacing every 'delegate' with 'function' makes your code 
>> work.
>>
>> Ali
>

i got it with function, thx Ali

-------------- code ----------
import std.stdio;
import std.typecons : Tuple;

struct attribute
{
     public bool function( int ) predicate;
     public this(  bool function( int ) pred )
     {
         predicate = pred;
     }
}

struct Data
{
     @attribute( (int a) => a == 42 )
     int x;
     @attribute( (int a) => a == 8 )
     int y;
}



void main()
{

     bool function( int ) tmp;

     pragma( msg, __traits(getAttributes, Data.x)[0] );
     pragma( msg, __traits(getAttributes, Data.x)[0].predicate( 42 
) );
     tmp = __traits(getAttributes, Data.x)[0].predicate;
     writeln( tmp(42 ) );
}


More information about the Digitalmars-d-learn mailing list