Figuring out the returntype opDipatch

TheFlyingFiddle theflyingfiddle at gmail.com
Sat Nov 2 22:45:28 PDT 2013


> struct S
> {
>     R opDispatch(string name, R, T...)(T parameters)
>     {
>         return R.init;
>     }
> }
>
> void main()
> {
>     auto s = S();
>
>     auto r0 = s.foo!("foo", int)("hello");    // does not 
> compile
>     auto r1 = s.bar!("bar", double)(100);
>
>     static assert (is (typeof(r0) == int));
>     static assert (is (typeof(r1) == double));
> }
>
> Ali

I decided to go with the following:
interface IReflectionable
{
     final void call(string name, Params...)(Params params)
     {
         //code for getting pointer.
     }

     final void call(Params...)(string name, Params params)
     {
         //code for getting pointer.
     }

     final R call(R, Params...)(string name, Params params)
     {
         //code for getting pointer.
         return R.init;
     }

     final R call(R, string method,  Params...)(Params params)
     {
         //code for getting pointer.
         return R.init;
     }
}

unittest
{
     auto foo = new Foo();

     string hello = "Hello";
     int a = 10;

     foo.call!("bar")(a); //Infers void return type and int 
parameter.
     auto r1 = foo.call!(int, "baz")(hello); //Return type is int 
and infers string parameter.

     foo.call("bar", a); //Infers void return type and int 
parameter.
     foo.call!(int)("baz", hello); //Return type is int and infers 
sting parameter.
}

Sadly i could not get opDispatch to work either :( Idealy i would 
like to have foo.baz!(int)(hello) working but i guess since it's 
a template the compiler is
unable to convert it into foo.opDispatch!("baz", int)(hello);

Thanks for all the help Ali.


More information about the Digitalmars-d-learn mailing list