www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to get the "this" ptr of a lambda inside the lambda?

reply Rufus Smith <RufusSmith indi.com> writes:
Error: 'this' is only defined in non-static member functions, not 
__lambda2	

Lambda's are delegates and delegates have a "this" type of 
pointer. I would like to get at it inside the lambda to check for 
some things. I'm doing some funky stuff. I'm not concerned about 
the scope or what this actually pointers to or anything like 
that, just need it's value for debugging.

Thanks.
Jul 18 2016
parent reply Mike Parker <aldacron gmail.com> writes:
On Tuesday, 19 July 2016 at 06:32:32 UTC, Rufus Smith wrote:
 Error: 'this' is only defined in non-static member functions, 
 not __lambda2	

 Lambda's are delegates and delegates have a "this" type of 
 pointer. I would like to get at it inside the lambda to check 
 for some things. I'm doing some funky stuff. I'm not concerned 
 about the scope or what this actually pointers to or anything 
 like that, just need it's value for debugging.
No, delegates do not have a "this" type of pointer. "this" is an implicit function parameter in a class or struct member function. Delegates have no such thing. The only generic way I know of to get at a delegate's function pointer inside the implementation is to explicitly add the pointer type to the parameter list as part of the declaration and pass it as an argument when you call the delegate.
Jul 18 2016
parent reply Rufus Smith <RufusSmith indi.com> writes:
On Tuesday, 19 July 2016 at 06:46:44 UTC, Mike Parker wrote:
 On Tuesday, 19 July 2016 at 06:32:32 UTC, Rufus Smith wrote:
 Error: 'this' is only defined in non-static member functions, 
 not __lambda2	

 Lambda's are delegates and delegates have a "this" type of 
 pointer. I would like to get at it inside the lambda to check 
 for some things. I'm doing some funky stuff. I'm not concerned 
 about the scope or what this actually pointers to or anything 
 like that, just need it's value for debugging.
No, delegates do not have a "this" type of pointer. "this" is an implicit function parameter in a class or struct member function. Delegates have no such thing. The only generic way I know of to get at a delegate's function pointer inside the implementation is to explicitly add the pointer type to the parameter list as part of the declaration and pass it as an argument when you call the delegate.
Delegates do have a this, they have a context pointer that is implicitly passed and used to access the outside context. It is no different than methods. Just because the explicit implementation details are different does not change the underlying meaning.
Jul 19 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 7/19/16 11:25 AM, Rufus Smith wrote:
 On Tuesday, 19 July 2016 at 06:46:44 UTC, Mike Parker wrote:
 On Tuesday, 19 July 2016 at 06:32:32 UTC, Rufus Smith wrote:
 Error: 'this' is only defined in non-static member functions, not
 __lambda2

 Lambda's are delegates and delegates have a "this" type of pointer. I
 would like to get at it inside the lambda to check for some things.
 I'm doing some funky stuff. I'm not concerned about the scope or what
 this actually pointers to or anything like that, just need it's value
 for debugging.
No, delegates do not have a "this" type of pointer. "this" is an implicit function parameter in a class or struct member function. Delegates have no such thing. The only generic way I know of to get at a delegate's function pointer inside the implementation is to explicitly add the pointer type to the parameter list as part of the declaration and pass it as an argument when you call the delegate.
Delegates do have a this, they have a context pointer that is implicitly passed and used to access the outside context. It is no different than methods. Just because the explicit implementation details are different does not change the underlying meaning.
I think what Mike may be alluding to is that there is no name for the stack frame pointer you can use. There is no 'this' pointer that you can get at (even though it can be passed). Also note that lambdas are not necessarily delegates, they could be straight function pointers if they don't need a context: void main() { int a; pragma(msg, typeof((int b) => b * 2)); // int function(int b) pure nothrow nogc safe pragma(msg, typeof(() => a * 2)); // int delegate() pure nothrow nogc safe } A question to ask is, why do you need it? -Steve
Jul 19 2016
parent reply Rufus Smith <RufusSmith indi.com> writes:
On Tuesday, 19 July 2016 at 15:58:49 UTC, Steven Schveighoffer 
wrote:
 On 7/19/16 11:25 AM, Rufus Smith wrote:
[...]
I think what Mike may be alluding to is that there is no name for the stack frame pointer you can use. There is no 'this' pointer that you can get at (even though it can be passed). Also note that lambdas are not necessarily delegates, they could be straight function pointers if they don't need a context: void main() { int a; pragma(msg, typeof((int b) => b * 2)); // int function(int b) pure nothrow nogc safe pragma(msg, typeof(() => a * 2)); // int delegate() pure nothrow nogc safe }
Yes, but then this = null. I matters not for my use case.
 A question to ask is, why do you need it?
Magic my friend! Magic!!!
Jul 19 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 7/19/16 12:52 PM, Rufus Smith wrote:
 On Tuesday, 19 July 2016 at 15:58:49 UTC, Steven Schveighoffer wrote:
 On 7/19/16 11:25 AM, Rufus Smith wrote:
 [...]
I think what Mike may be alluding to is that there is no name for the stack frame pointer you can use. There is no 'this' pointer that you can get at (even though it can be passed). Also note that lambdas are not necessarily delegates, they could be straight function pointers if they don't need a context: void main() { int a; pragma(msg, typeof((int b) => b * 2)); // int function(int b) pure nothrow nogc safe pragma(msg, typeof(() => a * 2)); // int delegate() pure nothrow nogc safe }
Yes, but then this = null. I matters not for my use case.
'this' is not null in either case. There is no 'this'. There is probably a way to get the stack pointer. Take a look at the code in std.functional.toDelegate. http://dlang.org/phobos/std_functional.html#toDelegate -Steve
Jul 19 2016
parent reply Rufus Smith <RufusSmith indi.com> writes:
On Tuesday, 19 July 2016 at 16:58:12 UTC, Steven Schveighoffer 
wrote:
 On 7/19/16 12:52 PM, Rufus Smith wrote:
 On Tuesday, 19 July 2016 at 15:58:49 UTC, Steven Schveighoffer 
 wrote:
[...]
Yes, but then this = null. I matters not for my use case.
'this' is not null in either case. There is no 'this'.
Please stop saying that: https://en.wikipedia.org/wiki/This_(computer_programming) this is more general than you think.
Jul 19 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 7/19/16 1:03 PM, Rufus Smith wrote:
 On Tuesday, 19 July 2016 at 16:58:12 UTC, Steven Schveighoffer wrote:
 On 7/19/16 12:52 PM, Rufus Smith wrote:
 On Tuesday, 19 July 2016 at 15:58:49 UTC, Steven Schveighoffer wrote:
 [...]
Yes, but then this = null. I matters not for my use case.
'this' is not null in either case. There is no 'this'.
Please stop saying that: https://en.wikipedia.org/wiki/This_(computer_programming) this is more general than you think.
The compiler knows about the context pointer, your delegate has no name for it. And in a lambda that does not need one, there is no context pointer, it's not a pointer that's set to null. I fully understand what you're looking for. In D, 'this' means the object/struct that a method is being called with. If you don't have an object delegate, then you don't have a 'this' reference (and by that I mean a named parameter to the member function called 'this' or any other name). -Steve
Jul 19 2016