Calling an alias for a Class method in another scope

Steven Schveighoffer schveiguy at yahoo.com
Fri Jun 1 07:30:20 PDT 2012


On Fri, 01 Jun 2012 10:14:26 -0400, Jacob Carlborg <doob at me.com> wrote:

> On 2012-06-01 15:51, Steven Schveighoffer wrote:
>> On Fri, 01 Jun 2012 09:25:57 -0400, d coder <dlang.coder at gmail.com>  
>> wrote:
>>> Class Bar(alias F) {
>>> // Call F in some function here
>>> }
>>>
>>> Class Foo {
>>> void foo();
>>> Bar!(() {foo();}) bar;
>>> }
>>>
>>> Again this does not work. Maybe I am expecting too much from D. :-) I  
>>> am
>>> somewhat aware of the issues involved here. I have seen some earlier D
>>> threads.
>>
>> lambdas cannot exist outside a function scope. This is a limitation of D
>> that has been pointed out before, and I think it should be made
>> available, but don't hold your breath :) D is just starting to get more
>> filled out in the area of lambdas.
>
> That can't work since there is no this-reference for "foo"?

I'm surprised at some things that work with alias.  It sometimes seems  
like black magic.

Consider that this does work:

void incx(alias x)()
{
     x++;
}

void main()
{
     int x = 0;
     incx!x();
     assert(x == 1);
}


now, consider that when incx!x is compiled, the stack frame for main  
doesn't exist!  So how does the compiler build it?

I suppose the issue with the lambda in class scope issue above is  
different, because incx has an implicit reference back to its caller's  
frame through the stack pointer.

But it seems like there may be a way that it could work.  For example,  
create an implicit, final (private) member function which has the body of  
the lambda.  Then construct the Bar type with an alias to that function.   
Would it need a stack frame to work?  I have no idea :)  Again, black  
magic.

-Steve


More information about the Digitalmars-d mailing list