opDispatch/template get this
Inquie via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Apr 3 14:49:07 PDT 2017
I am using opDispatch to wrap function calls
Error: 'this' is only defined in non-static member functions, not
opDispatch!"foo"
class X
{
auto localfoo() { return 3; }
template opDispatch(string name, Args...)
{
static if (name == `foo`) { alias opDispatch = () {
return this.localfoo();
};
}
which doesn't work because of this
I tried
template opDispatch(string name, this _this, Args...)
{
static if (name == `foo`) { alias opDispatch = () { return
_this.localfoo(); };
}
but that doesn't work either ;/
I call it like
auto y = x.foo();
but foo isn't found
https://dpaste.dzfl.pl/bf31f535340f
class X
{
auto localfoo() { return 3; }
auto localfoo2(int x) { return x; }
template opDispatch(string name, Args...)
{
static if (name == `foo`)
{
alias opDispatch = () { return this.localfoo(); };
}
static if (name == `bar`)
{
alias opDispatch = () { return this.localfoo2(); }; // need to
be able to pass Args properly here
}
}
}
void main()
{
auto x = new X();
auto z = x.localfoo();
auto y = x.foo();
auto q = x.bar();
}
More information about the Digitalmars-d-learn
mailing list