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

Steven Schveighoffer schveiguy at gmail.com
Fri Mar 6 13:55:25 UTC 2020


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


More information about the Digitalmars-d-learn mailing list