does alias this work correctly?

Zhenya zheny at list.ru
Mon Jan 14 08:57:03 PST 2013


On Sunday, 13 January 2013 at 22:36:03 UTC, Jonathan M Davis 
wrote:
> On Sunday, January 13, 2013 20:41:48 Zhenya wrote:
>> On Sunday, 13 January 2013 at 19:35:08 UTC, Maxim Fomin wrote:
>> > According to spec http://dlang.org/class.html#AliasThis
>> > undefined lookups are forwarded to AliasThis member. But in
>> > your case Foo struct has bar member, so no further resolution
>> > is performed. Rename the member and the code compiles.
>> > 
>> > Note, this seems to come from neighbor thread
>> > http://forum.dlang.org/thread/uelmpwwckcveimpbdtdo@forum.dlang.org.
>> > I think it is simpler to rename functions than create bunch 
>> > of
>> > structs just to be able to have eponymous non-static and 
>> > static
>> > functions.
>> 
>> I just want very much avoid renaming function,it's principle 
>> for
>> me.
>> So I would like to know is my sample right or no.
>
> It functions exactly like it's supposed to. As Maxim said, 
> undefined lookups go
> to the alias this member. If Foo already has a bar and you try 
> and call it, it
> doesn't matter what Foo is aliased to, it's Foo's bar that's 
> going to be used.
>
> - Jonathan M Davis

import std.stdio;

struct Bar
{
	void opDispatch(string op)()
		if(op == "bar")
		{
			if(this !is m_init)
				writeln("non-static");
			else
				writeln("maybe static");
		}
	static @property Bar m_init()
	{
		return Bar.init;
	}
	alias m_init this;
}

void main()
{
	Bar b;
	Bar.bar();
	readln;
}

If I understood you correctly, it must be compiled since there 
isn't some 'bar' that is going to be used, right?
But it doesn't compile.


More information about the Digitalmars-d-learn mailing list