On 06/03/2014 05:57 AM, bioinfornatics wrote:

> On Tuesday, 3 June 2014 at 12:27:45 UTC, bioinfornatics wrote:

>>>> 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;
> //~ import std.functional : toDelegate;
> //~ struct attribute( alias Pred )
> //~ {
> //~     public:
> //~     alias Predicate = Pred;
> //~ }
>
> struct attribute
> {
>      public bool delegate( int ) predicate;
>      public this(  bool delegate( int ) pred )
>      {
>          predicate = pred;
>      }
> }
>
> struct Data
> {
>      @attribute( (int a) => a == 42 )
>      int x;
> }
>
>
>
> void main()
> {
>
>      bool delegate( int ) tmp;
>
>      pragma( msg, __traits(getAttributes, Data.x)[0] );
>      pragma( msg, __traits(getAttributes, Data.x)[0].predicate( 42 ) );
>
> pragma( msg, __traits(getAttributes, Data.x)[0].predicate.stringof );
>      tmp = __traits(getAttributes, Data.x)[0].predicate;
>      writeln( tmp(42 ) );
>      //~tmp = toDelegate(__traits(getAttributes, Data.x)[0].Predicate);
> }
> ----------------------------------
>
>
>
>
> give at compile time this:
>
> attribute(function (int a) => a == 42)
> true
> (attribute __ctmp1474;
>   , __ctmp1474).this(function (int a) => a == 42).predicate
>
>
> And segfault at run-time

A 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

Reply via email to