digitalmars.D.learn - How to assign a delegate to a var ?
- bioinfornatics (26/26) Jun 02 2014 Hi,
- Meta (5/32) Jun 02 2014 The problem is that you're missing the type of a in "(a) => a ==
- bioinfornatics (11/59) Jun 03 2014 with this i got an error even if i do public alias Predicate in
- bioinfornatics (44/105) Jun 03 2014 To have same code
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/56) Jun 03 2014 A delegate has a context pointer that it uses when executing at
- bioinfornatics (14/90) Jun 03 2014 The only reason that is because i do nor know how to do:
- bioinfornatics (29/39) Jun 03 2014 i got it with function, thx Ali
- Jesse Phillips (9/11) Jun 03 2014 D will decide the type based on if context is needed. If you look
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 ------------
Jun 02 2014
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
Jun 02 2014
On Monday, 2 June 2014 at 23:27:03 UTC, Meta wrote:On Monday, 2 June 2014 at 22:18:39 UTC, bioinfornatics wrote: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; }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
Jun 03 2014
On Tuesday, 3 June 2014 at 12:27:45 UTC, bioinfornatics wrote:On Monday, 2 June 2014 at 23:27:03 UTC, Meta wrote:To have same code ------------- 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-timeOn Monday, 2 June 2014 at 22:18:39 UTC, bioinfornatics wrote: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; }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
Jun 03 2014
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-timeA 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
Jun 03 2014
On Tuesday, 3 June 2014 at 15:00:05 UTC, Ali Çehreli wrote:On 06/03/2014 05:57 AM, bioinfornatics wrote:The only reason that is because i do nor know how to do: attribute( (int a) => a == 42 ) int x; without using a delegate, with a function that will not work as (int a) => a == 42 is a delegate no ? struct attribute { public bool function( int ) predicate; public this( bool function( int ) pred ) { predicate = pred; } }On Tuesday, 3 June 2014 at 12:27:45 UTC, bioinfornatics wrote:try iI would like store the delegate to another var but when isomewhere.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»------------- 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-timeA 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
Jun 03 2014
delegate has a context pointer that it uses when executingi 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 ) ); }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
Jun 03 2014
On Tuesday, 3 June 2014 at 15:14:06 UTC, bioinfornatics wrote:without using a delegate, with a function that will not work as (int a) => a == 42 is a delegate no ?D will decide the type based on if context is needed. If you look at the output you had: , __ctmp1474).this(function (int a) => a == 42).predicate Inside this, you'll see that your lambda was identified as a function. I'm guessing that having the function assigned to a delegate has confused the compiler which is why segfault. I think such should be filed as a bug, likely should not have compiled.
Jun 03 2014