On Monday, 2 June 2014 at 23:27:03 UTC, Meta wrote:
On Monday, 2 June 2014 at 22:18:39 UTC, bioinfornatics wrote:
Hi,

I would like store the delegate to another var but when i try i
get:


testTraitsWithDelegate.d(13): Error: expression template
__lambda2 is void and has no value

I do not want to run it only to save the «function» somewhere.


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

struct attribute( alias Pred )
{
    alias Predicate = Pred;
}



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



void main()
{

    bool delegate( int ) tmp;

pragma( msg, __traits(getAttributes, Data.x)[0].Predicate( 42
) );

    tmp = __traits(getAttributes, Data.x)[0].Predicate;
}
----------- END CODE ------------

The problem is that you're missing the type of a in "(a) => a == 42". Without the type, it's actually a template lambda, not a lambda function. Try changing it to "(int a) => a == 42".
auto

with this i got an error even if i do public alias Predicate in
struct attribute:
Error: struct testTraitsWithDelegate.attribute!(function (int a)
=> a == 42).attribute member __lambda2 is not accessible

and i fail to do same with

struct attribute
{
   public:
     static bool delegate( int ) Predicate;
}

Reply via email to