On Tuesday, 15 January 2013 at 00:04:15 UTC, Jonathan M Davis
wrote:
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
:( If compiler tried alias this before opDispatch all would be OK.
Maybe I'm going to give up.
Thank you for comprehensive explanation.