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

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 19 09:58:12 PDT 2016


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


More information about the Digitalmars-d-learn mailing list