Pseudo namespaces

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 3 18:34:05 PST 2015


On 12/04/2015 03:24 AM, Tofu Ninja wrote:
> On Friday, 4 December 2015 at 02:02:48 UTC, Timon Gehr wrote:
>> On 12/04/2015 02:37 AM, Mike wrote:
>>> On Friday, 4 December 2015 at 01:04:33 UTC, Idan Arye wrote:
>>>>
>>>> People are going to hate me, but http://dpaste.dzfl.pl/851d1d1f5e4b
>>>
>>> Doesn't seem to scale to member access:
>>> http://dpaste.dzfl.pl/37193377524c
>>>
>>> /d649/f987.d-mixin-3(7): Error: 'this' is only defined in non-static
>>> member functions, not fun
>>>
>>> Is there a way to make it work?
>>>
>>> Mike
>>
>> template namespace(string code,alias a=void){
>>     mixin(code);
>> }
>>
>> struct List{
>>     int x;
>>     alias stable = namespace!(q{
>>         void fun(){
>>             import std.stdio;
>>             writeln(this.x);
>>         }
>>     },x);
>> }
>>
>> void main(){
>>     List lst;
>>     lst.stable.fun();
>> }
>>
>>
>> :o)
>
> WTF! Why does that even work! That is strait madness! When the code gets
> mixed into the namespace template, x would have been re-aliased to a, it
> would have made sense to write "writeln(a)" which works as well... but
> "writeln(this.x)", wut, how... that makes no sense...

http://dlang.org/spec/template.html#nested-templates

"If a template has a template alias parameter, and is instantiated with 
a local symbol, the instantiated function will implicitly become nested 
in order to access runtime data of the given local symbol."

You are probably relying on this feature in a significant portion of 
your code, especially for lambda template arguments. It does not really 
cover all cases where one would like to have nested instantiation though.


More information about the Digitalmars-d mailing list