Figuring out the returntype opDipatch

TheFlyingFiddle theflyingfiddle at gmail.com
Sat Nov 2 19:15:57 PDT 2013


> Can you provide a little more complete code please. Otherwise, 
> the return type is available to typeof:
>
> import std.stdio;
>
> struct S
> {
>     auto opDispatch(string name, T...)(T parameters)
>     {
>         writef("S.%s:", name);
>         foreach (parameter; parameters) {
>             writef(" %s", parameter);
>         }
>         writeln();
>
>         return parameters[$/2];
>     }
> }
>
> void main()
> {
>     auto s = S();
>
>     auto r0 = s.foo(42, 1.5, "hi");
>     auto r1 = s.bar("hello", 'a', 100);
>
>     static assert (is (typeof(r0) == double));
>     static assert (is (typeof(r1) == char));
> }
>
> Ali

In the IReflectionable interface:

interface IReflectionable
{
     final P funcPtr(P)(string fun) if (is(P == delegate))
     {
         //Using mangeling for overloads and type safety
	auto ptr = delPtr_impl(mangle!P(fun));

	P del;
	del.ptr = cast(typeof(del.ptr))ptr[0];
	del.funcptr = cast(typeof(del.funcptr))ptr[1];
	return del;
     }

     final P funcPtr(P)(string fun) if (is(P == function))
     {
         //Using mangeling for overloads and type safety
	auto ptr = funcPtr_impl(mangle!(P)(fun));
	return cast(P)ptr;
     }

     final ?? opDispatch(string name, Params...)(Params params)
     {
        alias ?? delegate(Params) del_type;
        auto del = funcPtr!(del_type)(name);

        static if(?? == void)
           del(params);
        else
           return del(params);
     }

     protected Tuple!(void*, void*) delPtr_impl(string s);
     protected void* funcPtr_impl(string s);
}

What i'm interested in is determining what type ?? should be. 
Variant works but if possible i would like to avoid it. This 
would require me to know about the code at the invokation site. 
(So i'm guessing it might not be possible)


More information about the Digitalmars-d-learn mailing list