www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opDispatch/template get this

reply Inquie <Inquie data.com> writes:
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();
}
Apr 03
parent John Chapman <johnch_atms hotmail.com> writes:
On Monday, 3 April 2017 at 21:49:07 UTC, Inquie wrote:
 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();
 }
Make opDispatch a templated function and forward with __traits(getMember, this, "foo") or a mixin. class X { auto localfoo() { return 3; } auto localfoo2(int x) { return x; } auto opDispatch(string name, Args...)(auto ref Args args) { static if (name == "foo") return __traits(getMember, this, "localfoo")(args); else static if (name == "bar") mixin("return localfoo2(args);"); } }
Apr 04