Figuring out the returntype opDipatch

TheFlyingFiddle theflyingfiddle at gmail.com
Thu Nov 14 12:40:44 PST 2013


On Thursday, 14 November 2013 at 20:39:35 UTC, TheFlyingFiddle 
wrote:
> On Sunday, 3 November 2013 at 10:48:45 UTC, Jacob Carlborg 
> wrote:
>> On 2013-11-03 03:15, TheFlyingFiddle wrote:
>>
>>> 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)
>>
>> I have the same problem as well. I haven't figured out the 
>> best way to solve this yet.
>
> Might this be something you could solve using the DIP50 AST 
> macros?
>
> Something like
>
>
> interface IRefectionable
> {
>    //I'm assuming you can use a macro as opDispatch
>    macro opDispatch(string s, Params...)(Context context, 
> Ast!(Params) ast)
>    {
>        //The name is sort of bad
>        enum returnType = context.?? //Look at the stuff in the 
> context to figure out what the returnType would be.
>
>        return <|
>    alias ?? delegate(Params) del_type;
>        auto del = funcPtr!(del_type)(name);
>
>        static if(?? == void)
>           del(params);
>        else
>           return del(params);
>    }
> }
>
> unittest
> {
>    IReflectionable refl = new Foo();
>
>    refl.bar(1); //void is infered since not setting any value.
>    int baz = refl.baz("Hello");
>    auto baz2 = refl.baz("hello"); //Can't work.
>
> }

Edit: ?? should be replaced with $returnType



More information about the Digitalmars-d-learn mailing list