ADL
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Fri Sep 2 05:43:52 PDT 2016
On 9/2/16 8:15 AM, Manu via Digitalmars-d wrote:
> In C++, there is this ADL thing (argument dependent lookup).
> What it does is, when searching for overloads, in addition to looking
> in the local namespace, it also looks in the namespace of the function
> arguments.
>
> D doesn't seem to have this, and that is proving to be quite problematic.
> What's the work around?
>
> C++ example:
>
> namespace bob {
> struct S {};
> void f(S s);
> }
>
> namespace joe {
> struct T {};
> void f(T t);
>
> void test()
> {
> T t;
> f(t); // obviously works, T is in the local namespace
>
> bob::S s;
> f(s); // local namespace can't see `void f(S)`, but given the
> argument 's', which is typed bob::S, it will search the bob::
> namespace for overloads of f(), so this code compiles successfully.
> }
> }
>
>
> I have the same configuration across 2 modules in D.
> In one module, I receive the foreign modules type via template arg,
> but I haven't imported that type's module, so when I try to call the
> function it can't find the overload, because it's not imported, and it
> doesn't search the argument type's module (ie, namespace) for
> overloads.
>
This is a limitation, you can only use struct members given a type, you
can't use UFCS functions or regular calls like the above. You need to
import the module in the template *definition* file, which is problematic.
I think I remember seeing vibe.d do some funky shit to work around this,
like getting the fully qualified name, and using string mixins to import
the module that defines that type.
I think it would be nice to see this fixed, but I'm not sure of the
implications.
One possibility:
import __traits(moduleOf, T);
Then you could do this import as a local import when you need it. Of
course, this is not as nice as just having the compiler do this
automatically.
-Steve
More information about the Digitalmars-d
mailing list