How to dispatch a class function for an object accessed by handle?

wjoe invalid at example.com
Fri Mar 6 14:44:49 UTC 2020


On Friday, 6 March 2020 at 13:55:25 UTC, Steven Schveighoffer 
wrote:
> On 3/6/20 6:51 AM, wjoe wrote:
>> On Thursday, 5 March 2020 at 18:33:41 UTC, Adam D. Ruppe wrote:
>>> On Thursday, 5 March 2020 at 14:24:33 UTC, wjoe wrote:
>>>
>>> [...]
>>>
>>> template opDispatch(string name) {
>>>     auto opDispatch(T, Args...)(Args args) {
>>>        ...
>>>     }
>>> }
>>>
>>> [...]
>>>
>>>
>>> NOTE: opDispatch suppresses internal compile errors, it will 
>>> just say "no such property whatever". you can explicitly 
>>> instantiate with `f.opDispatch!"whatever` to help see better 
>>> errors.
>>>
>> 
>> Follow-up question:
>> 
>> Calling f.whatever!SomeResource(...); works no problem.
>> However I can't figure out how to call a function by 
>> explicitly instantiating opDispatch.
>> 
>> Since f.opDispatch!"load"(handle, "wallpaper.png");
>> doesn't compile, I refreshed my memory about the shortcut 
>> syntax and the eponymous syntax and the way I read it is that 
>> this is a template of a template.
>> 
>> So I tried this: f.opDispatch!"load".opDispatch!Bitmap(handle, 
>> "path/to/wallpaper.png");
>
> This doesn't work, because an eponymous template does not 
> provide access to the internals of the template.
>
>> 
>> But this doesn't compile either and errors out with:
>> Error: Cannot resolve type for f.opDispatch(T, 
>> ARGS...)(ResourceHandle handle, ARGS args)
>> 
>> I don't understand this error message. Which type can't be 
>> resolved?
>> 
>> Is there a way to look at output of what the compiler 
>> generates for f.whatever!SomeResource(...); ?
>
> You can use -vcg-ast, but this isn't necessarily going to be 
> compilable code.
>
> D doesn't allow chained instantiation (i.e. (A!B)!C), so you 
> need to use either a mixin or a helper:
>
> import std.meta;
>
> enum fname = "load";
>
> Instantiate!(f.opDispatch!fname, 
> Bitmap)("path/to/wallpaper.png")
>
> or
>
> mixin("f." ~ fname ~ "!(Bitmap)(...);");
>
> I'm assuming fname is given to you as a compile-time string and 
> that's why you'd need to run opDispatch manually.
>
> -Steve

I tried Instantiate this morning after I found a reply you made 
to someone else who was trying to chain instantiate. But for the 
reason you stated it didn't work.

Funny you mention: mixin("f." ~ fname ~ "!(Bitmap)(...);");
Because that's like my initial implementation :)

As for the the command line switch. My plan wasn't to copy paste. 
Sometimes I have a hard time to comprehend results because it's 
like I've put water, flour and eggs on the table. Then I'm 
presented with a loaf of bread and I'm baffled. Then I want to 
understand what happened between putting the ingredients on the 
table and the ready baked loaf.

Anyways, thanks for your answers.


More information about the Digitalmars-d-learn mailing list