ADL

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 3 06:20:54 PDT 2016


On 2016-09-03 13:16, Walter Bright wrote:

>> It's mostly about how
>> templates specify what interface they require and how the requirements
>> are
>> satisfied by the caller. ADL is a workaround for the lack of a
>> convenient enough
>> such protocol in templates. Other approaches to generics solve this
>> particular
>> issue quite elegantly. (E.g. type classes implicitly pass along the
>> required
>> free-function functionality.) D's templates don't, this is why it is a
>> template
>> issue.
>>
>> By default, name lookup does not work in a way that would allow you to
>> actually
>> extend types using UFCS, and therefore none of Phobos works that way.
>
> Lambdas!

So, something like this:

module foo;

struct Foo {}
int front(Foo f);
void popFront(Foo f);
bool empty(Foo f);

module algo;

void algorithm(alias front, alias popFront, alias empty, T)(T t);

module user;

import foo;
import algo;

void main()
{
     Foo f;
     algorithm!(() => f.front, () => f.popFront(), () => f.empty)(f);
}

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list