Pseudo namespaces

Tofu Ninja via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 3 18:31:33 PST 2015


On Friday, 4 December 2015 at 02:24:32 UTC, 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...

WTF, some how having an alias to x passed in, brings the entire 
this along with it. This works equally as well....

struct List{
	int x;
	int y;
	alias stable = namespace!(q{
			void fun(){
				import std.stdio;
				writeln(typeid(this));
                                 writeln(this.x + this.y);
			}
		},x);
}


More information about the Digitalmars-d mailing list