Making external types available to mixins

Eduard Staniloiu edi33416 at gmail.com
Thu Nov 22 16:27:08 UTC 2018


On Sunday, 18 November 2018 at 11:29:51 UTC, John Chapman wrote:
> On Saturday, 17 November 2018 at 21:11:38 UTC, Adam D. Ruppe 
> wrote:
>> On Saturday, 17 November 2018 at 17:58:54 UTC, John Chapman 
>> wrote:
>>> Has anyone had a similar need and come up with a solution?
>>
>> You might be able to just pass it the Calendar type, and then 
>> fetch its parent module and get the ICalendarFactory from 
>> there (assuming they are defined in the same module).
>>
>> But generally speaking, passing strings to a mixin that refer 
>> to something in another module isn't going to work well thanks 
>> to scoping rules. You are better off passing a symbol of some 
>> sort.
>
> So there is no actual Calendar type. There's an 
> ICalendarFactory type that creates instances of ICalendar 
> (these types are part of a third-party API).  "Calendar" is 
> just a key users could use when calling a "makeWith" method 
> that would build the ICalendar/Factory names, instantiate the 
> factory, call the appropriate factory method and return the 
> result. There are thousands of such object/factory pairs in the 
> API. Just trying to cut out a lot of boilerplate code, but it 
> doesn't seem doable this way.

Cheers,

So I had a go at this and I have a working solution.
https://run.dlang.io/is/oaH6Ib

At first, I tried to do everything in the mixin, as you can see 
with the `failedAttempt` function. The idea was that this should 
have worked like `mixin(failedAttempt!"Calendar"(1, 2, 3));`. As 
you can see, and the name suggests, I wasn't able to make it work 
with `args`.

The solution I have to your problem is to use a template, in this 
case the `theType` template that will expand to the fully 
qualified name. So you'd use it like
`makeWith!(theType!"Calendar")(args);`

Hope it helps!

Edi


More information about the Digitalmars-d-learn mailing list