www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - opDispatch to template members

reply "Phil Lavoie" <maidenphil hotmail.com> writes:
Greetings,

Given the following code:

struct Inner {

   void fun1() {}
   void fun2( string toto ) {}
   string fun3() { return "tata"; }
   string fun4( string tutu ) { return "this is going too far"; }

   void fun5( string ohOh )() {}

}

struct Outer {
   private Inner _inner;

   auto opDispatch( string method, T... )( T args ) {
     return mixin( "_inner." ~ method )( args );
   }

}

void main( string[] args ) {
   Outer outer;
   outer.fun1();
   outer.fun2( "some string" );
   auto zeString = outer.fun3();
   auto zeOtherString = outer.fun4( zeString );
   outer.fun5!"popo"(); //No compilo amigo

}

Is it possible/desirable to ever get opDispatch to delegate to a 
template member function?

Thanks
Dec 06 2012
parent reply "Phil Lavoie" <maidenphil hotmail.com> writes:
I mean automatically dispatch to template members, not doing it 
in a case by case fashion (using template if clauses for example).

Thanks
Dec 06 2012
next sibling parent reply "deadalnix" <deadalnix gmail.com> writes:
On Thursday, 6 December 2012 at 21:49:21 UTC, Phil Lavoie wrote:
 I mean automatically dispatch to template members, not doing it 
 in a case by case fashion (using template if clauses for 
 example).

 Thanks
This is a known issue of the current design of opDispatch.
Dec 06 2012
next sibling parent "Phil Lavoie" <maidenphil hotmail.com> writes:
Got it!
Thanks
Dec 06 2012
prev sibling next sibling parent kenji hara <k.hara.pg gmail.com> writes:
Note that, std.typecons.Proxy implements such template member function
forwarding by opDispatch nesting.
If curious, you can look its implementation.

Kenji Hara

2012/12/7 deadalnix <deadalnix gmail.com>

 On Thursday, 6 December 2012 at 21:49:21 UTC, Phil Lavoie wrote:

 I mean automatically dispatch to template members, not doing it in a case
 by case fashion (using template if clauses for example).

 Thanks
This is a known issue of the current design of opDispatch.
Dec 06 2012
prev sibling parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Thursday, 6 December 2012 at 23:01:19 UTC, deadalnix wrote:
 On Thursday, 6 December 2012 at 21:49:21 UTC, Phil Lavoie wrote:
 I mean automatically dispatch to template members, not doing 
 it in a case by case fashion (using template if clauses for 
 example).

 Thanks
This is a known issue of the current design of opDispatch.
It seems like there should be a way to separate template function calls to opDispatch and keep opDispatch's own template half separate. Hmmm... Some type of separator. It would likely have to allow a beginning tuple, as well as call opDispatch that would recognize it as such. In theory... auto opDispatch(T..., string method, V...)(V args) Assuming it would be allowed, T represents the template portion of a call. Since the ... makes it illegal to be first (in normal cases) it may allow a special rule in order to understand it's a passable tuple for templates only, and allowed for only the first and last arguments. Or would T[] be better? Then calling such an opDispatch may be... { static assert (!T.length) { return mixin("._inner." ~ method)(args); } else { return mixin("._inner." ~ method)!(T)(args); } } The rewrite of such a function call would be.. auto zeOtherString = outer.fun4( zeString ); outer.fun5!"popo"(); //No compilo amigo would become (I think?) //depending on how they do it... auto zeOtherString = outer.opDispatch("fun4")(zeString); auto zeOtherString = outer.opDispatch([], "fun4")(zeString); auto zeOtherString = outer.opDispatch(void[0], "fun4")(zeString); outer.opDispatch(["popo"], "fun5")(); Having the passable tuple after the method may be confusing, but might equally work... auto opDispatch(string method, T t, V...)(V args) (or is it 'T[] t'?) Allowing a blank (for t) wouldn't probably be allowed, so two opDispatches might have to be made, but that might be minor. Depends on what makes sense and how walter/andrei decide to handle if (if at all). If it still requires a separating type, you could always use void... auto opDispatch(T[], void, string method, V...)(V args) outer.opDispatch(["popo"], void, "fun5")(); or?? auto opDispatch(string method, T[], void, V...)(V args) //void and non-void attempted? Or always insert void silently? outer.opDispatch("fun5", ["popo"])(); outer.opDispatch("fun5", ["popo"], void)();
Dec 08 2012
parent "deadalnix" <deadalnix gmail.com> writes:
On Sunday, 9 December 2012 at 03:33:07 UTC, Era Scarecrow wrote:
 On Thursday, 6 December 2012 at 23:01:19 UTC, deadalnix wrote:
 On Thursday, 6 December 2012 at 21:49:21 UTC, Phil Lavoie 
 wrote:
 I mean automatically dispatch to template members, not doing 
 it in a case by case fashion (using template if clauses for 
 example).

 Thanks
This is a known issue of the current design of opDispatch.
It seems like there should be a way to separate template function calls to opDispatch and keep opDispatch's own template half separate. Hmmm... Some type of separator.
It seems that using opDispatch within opDispatch as shown before in the thread solve the problem. This technique should be advertised.
Dec 10 2012
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-12-06 22:49, Phil Lavoie wrote:
 I mean automatically dispatch to template members, not doing it in a
 case by case fashion (using template if clauses for example).

 Thanks
As Kenji said, doing something like this: struct Outer { private Inner _inner; template opDispatch( string method ) { auto opDispatch (T... ) (T args) return mixin( "_inner." ~ method )( args ); } } } -- /Jacob Carlborg
Dec 06 2012
next sibling parent "Phil Lavoie" <maidenphil hotmail.com> writes:
Hi,

Indeed as he suggested. I tried to reply that I was looking at 
the code yesterday and that it was what I was looking for, but my 
post did not get through for some reasons.

Thanks!

On Friday, 7 December 2012 at 07:57:09 UTC, Jacob Carlborg wrote:
 On 2012-12-06 22:49, Phil Lavoie wrote:
 I mean automatically dispatch to template members, not doing 
 it in a
 case by case fashion (using template if clauses for example).

 Thanks
As Kenji said, doing something like this: struct Outer { private Inner _inner; template opDispatch( string method ) { auto opDispatch (T... ) (T args) return mixin( "_inner." ~ method )( args ); } } }
Dec 07 2012
prev sibling parent "Phil Lavoie" <maidenphil hotmail.com> writes:
Hi,

Yes indeed Kenji pointed me in the right direction. I tried 
posting that I looked at the code he suggested and found what I 
was looking for, but I had some issue posting :(.

Thanks



On Friday, 7 December 2012 at 07:57:09 UTC, Jacob Carlborg wrote:
 On 2012-12-06 22:49, Phil Lavoie wrote:
 I mean automatically dispatch to template members, not doing 
 it in a
 case by case fashion (using template if clauses for example).

 Thanks
As Kenji said, doing something like this: struct Outer { private Inner _inner; template opDispatch( string method ) { auto opDispatch (T... ) (T args) return mixin( "_inner." ~ method )( args ); } } }
Dec 07 2012