ADL

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Mon Sep 5 16:50:33 PDT 2016


On 05.09.2016 15:35, Andrei Alexandrescu wrote:
> On 9/5/16 11:43 AM, Timon Gehr wrote:
>> On 05.09.2016 06:05, Manu via Digitalmars-d wrote:
>>> An algorithm that calls a function on
>>> some T it receives just wants to look near the T; UFCS functions will
>>> be there.
>>
>> I agree with your post except for this.
>>
>> In general there could be four modules: one defines the type, one
>> defines the UFCS function, one defines the generic algorithm, and the
>> last one imports the former three. The problem is that D has no standard
>> way to make them work together.
>
> Do you think there should?

Probably. This kind of pattern has been very successful in other languages.

> And if so, what would the algorithm be? --
> Andrei

This is a good question. I'm not sure yet what the best solution is.

One hacky way is to provide a mixin template to create a wrapper type 
within each module that needs it, with std.typecons.Proxy. Proxy picks 
up UFCS functions in addition to member functions and turns them into 
member functions. But this leads to a lot of template bloat, because 
callers that share the same added UFCS functions don't actually share 
the instantiation. Also, it only works one level deep and automatically 
generated Wrapper types are generally prone to be somewhat brittle.

There is always the default option of requiring the user to create the 
Wrappers manually, but that's a lot of boilerplate. (Other languages can 
do analogous things in a very streamlined fashion, by not supporting 
arbitrary template constraints.)


More information about the Digitalmars-d mailing list