does alias this work correctly?

Jonathan M Davis jmdavisProg at gmx.com
Mon Jan 14 16:03:18 PST 2013


On Monday, January 14, 2013 17:57:03 Zhenya wrote:
> 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.

I honestly have no idea how opDispatch and alias this are supposed to 
interact. They're both used as fallbacks when the type doesn't directly define 
a function. That being said The reason that your code doesn't work here is the 
fact that you'r ecalling bar is if it were a static function, but your 
opDispatch isn't static. opDispatch would have to be static for it to be used 
as a static function (it can't be both static and non-static), and in that 
case, the if condition that you have in it wouldn't compile.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list