How to get the "this" ptr of a lambda inside the lambda?

2016-07-18 Thread Rufus Smith via Digitalmars-d-learn
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.




Re: How to get the "this" ptr of a lambda inside the lambda?

2016-07-18 Thread Mike Parker via Digitalmars-d-learn

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.


Re: How to get the "this" ptr of a lambda inside the lambda?

2016-07-19 Thread Rufus Smith via Digitalmars-d-learn

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.




Re: How to get the "this" ptr of a lambda inside the lambda?

2016-07-19 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Re: How to get the "this" ptr of a lambda inside the lambda?

2016-07-19 Thread Rufus Smith via Digitalmars-d-learn
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!!!




Re: How to get the "this" ptr of a lambda inside the lambda?

2016-07-19 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Re: How to get the "this" ptr of a lambda inside the lambda?

2016-07-19 Thread Rufus Smith via Digitalmars-d-learn
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.



Re: How to get the "this" ptr of a lambda inside the lambda?

2016-07-19 Thread Steven Schveighoffer via Digitalmars-d-learn

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