digitalmars.D.learn - How to get the "this" ptr of a lambda inside the lambda?
- Rufus Smith (8/8) Jul 18 2016 Error: 'this' is only defined in non-static member functions, not
- Mike Parker (8/15) Jul 18 2016 No, delegates do not have a "this" type of pointer. "this" is an
- Rufus Smith (6/23) Jul 19 2016 Delegates do have a this, they have a context pointer that is
- Steven Schveighoffer (16/38) Jul 19 2016 I think what Mike may be alluding to is that there is no name for the
- Rufus Smith (4/21) Jul 19 2016 Yes, but then this = null. I matters not for my use case.
- Steven Schveighoffer (6/27) Jul 19 2016 'this' is not null in either case. There is no 'this'.
- Rufus Smith (5/12) Jul 19 2016 Please stop saying that:
- Steven Schveighoffer (10/22) Jul 19 2016 The compiler knows about the context pointer, your delegate has no name
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
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
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: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.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 19 2016
On 7/19/16 11:25 AM, Rufus Smith wrote:On Tuesday, 19 July 2016 at 06:46:44 UTC, Mike Parker 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 } A question to ask is, why do you need it? -SteveOn Tuesday, 19 July 2016 at 06:32:32 UTC, Rufus Smith wrote: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.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 19 2016
On Tuesday, 19 July 2016 at 15:58:49 UTC, Steven Schveighoffer wrote:On 7/19/16 11:25 AM, Rufus Smith wrote:Yes, but then this = null. I matters not for my use case.[...]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?Magic my friend! Magic!!!
Jul 19 2016
On 7/19/16 12:52 PM, Rufus Smith wrote:On Tuesday, 19 July 2016 at 15:58:49 UTC, Steven Schveighoffer wrote:'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 -SteveOn 7/19/16 11:25 AM, Rufus Smith wrote:Yes, but then this = null. I matters not for my use case.[...]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 }
Jul 19 2016
On Tuesday, 19 July 2016 at 16:58:12 UTC, Steven Schveighoffer wrote:On 7/19/16 12:52 PM, Rufus Smith wrote:Please stop saying that: https://en.wikipedia.org/wiki/This_(computer_programming) this is more general than you think.On Tuesday, 19 July 2016 at 15:58:49 UTC, Steven Schveighoffer wrote:'this' is not null in either case. There is no 'this'.[...]Yes, but then this = null. I matters not for my use case.
Jul 19 2016
On 7/19/16 1:03 PM, Rufus Smith wrote:On Tuesday, 19 July 2016 at 16:58:12 UTC, Steven Schveighoffer wrote: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). -SteveOn 7/19/16 12:52 PM, Rufus Smith wrote:Please stop saying that: https://en.wikipedia.org/wiki/This_(computer_programming) this is more general than you think.On Tuesday, 19 July 2016 at 15:58:49 UTC, Steven Schveighoffer wrote:'this' is not null in either case. There is no 'this'.[...]Yes, but then this = null. I matters not for my use case.
Jul 19 2016