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
next sibling 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
parent 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
prev sibling next sibling parent "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
prev sibling 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:
--bcaec55407401f4f8204d0381031
Content-Type: text/plain; charset=UTF-8

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.

--bcaec55407401f4f8204d0381031 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Note that,=C2=A0std.typecons.Proxy implements such template member function= forwarding by opDispatch=C2=A0nesting.<div>If curious, you can look its im= plementation.</div><div class=3D"gmail_extra"><br></div><div class=3D"gmail= _extra">Kenji Hara<br> <br><div class=3D"gmail_quote">2012/12/7 deadalnix <span dir=3D"ltr">&lt;<a= href=3D"mailto:deadalnix gmail.com" target=3D"_blank">deadalnix gmail.com<= /a>&gt;</span><br><blockquote class=3D"gmail_quote" style=3D"margin-top:0px= ;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px= ;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1e= x"> <div class=3D"im">On Thursday, 6 December 2012 at 21:49:21 UTC, Phil Lavoie= wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin-top:0px;margin-right:0px;= margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color= :rgb(204,204,204);border-left-style:solid;padding-left:1ex"> I mean automatically dispatch to template members, not doing it in a case b= y case fashion (using template if clauses for example).<br> <br> Thanks<br> </blockquote> <br></div> This is a known issue of the current design of opDispatch.<br> </blockquote></div><br></div> --bcaec55407401f4f8204d0381031--
Dec 06 2012
prev sibling 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 next 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
prev sibling next sibling parent "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
prev sibling 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