Figuring out the returntype opDipatch

Ali Çehreli acehreli at yahoo.com
Sat Nov 2 21:09:47 PDT 2013


On 11/02/2013 03:41 PM, TheFlyingFiddle wrote:

 > The problem i am faced with is that i need a way to figure out the
 > return value of opDispatch by the invokation call.

If I now understand you correctly, what initially confused my was 
"invocation call." Because "invocation" makes me think about the actual 
arguments of the functino call.

 > all the information i need is here
 >
 > int a = foo.baz("hello");
 >
 > This gives returntype int.

So, what you are trying to do is to get the return type from the 
left-hand side of the assignment operator.

 > So is there a way to gain this information in opDspatch?

I don't think so. You can always return a special (similar to Variant 
that you mentioned) and play with automatic type conversions no that type.

 > Is it possible to do something like this?
 >
 > auto a = foo.baz!(int)("hello");

Hmmm. I don't think so. "baz" itself becomes the first argument of 
opDispatch. So I tried the following ugly thing but it does not work either:

import std.stdio;

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



More information about the Digitalmars-d-learn mailing list