ADL
Walter Bright via Digitalmars-d
digitalmars-d at puremagic.com
Fri Sep 2 18:09:18 PDT 2016
On 9/2/2016 4:51 PM, Manu via Digitalmars-d wrote:
> (I should have given the example with test() outside the namespace)
It's always best to provide an example of the actual problem rather than
something else.
> module bob;
> struct S {}
> void f(S s);
>
> module joe;
> struct T {}
> void f(T t);
>
> module myalgorithm;
> void test(T)(T t)
> {
> f(t);
> }
>
>
> module user_code;
> import bob, joe;
import myalgorithm; // needed
> void main()
> {
> test(S.init);
> test(T.init);
> }
>
> This is a better example. I can't be invading test() with any aliases,
> or imports. It wouldn't be an algorithm anymore if I did that.
> This pattern seems to bite me every direction I turn when trying to
> write range or algorithm style code. C++ has ADL, and ADL works. I've
> never thought about this problem in C++,
First solution:
module bob;
struct S {
void f();
}
Second solution:
module user_code;
import bob, joe;
import myalgorithm;
mixin myalgorithm.test!S;
mixin myalgorithm.test!T;
void main()
{
test(S.init);
test(T.init);
}
Third solution:
module myalgorithm;
void test(M,T)(T t)
{
M.f(t);
}
module user_code;
import bob, joe;
import myalgorithm;
void main()
{
test!bob(S.init);
test!joe(T.init);
}
Fourth solution:
module myalgorithm;
void test(T)(T t)
{
import std.traits;
mixin("import " ~ std.traits.moduleName!T ~ ";");
mixin("alias M = " ~ std.traits.moduleName!T ~ ";");
// The above could be encapsulated into an eponymous template
// that takes T as a parameter and returns the alias
M.f(t);
}
> or had any problems with ADL
https://en.wikipedia.org/wiki/Argument-dependent_name_lookup#Criticism
Essentially, ADL has awkward problems when getting beyond the simple cases. It
isn't right for D.
More information about the Digitalmars-d
mailing list