Koenig lookup in D?

monarch_dodra monarchdodra at gmail.com
Tue Aug 27 06:15:33 PDT 2013


On Tuesday, 27 August 2013 at 10:00:50 UTC, Peter Alexander wrote:
> On Tuesday, 27 August 2013 at 07:45:14 UTC, monarch_dodra wrote:
>> I think that D's non-hijack rules are pretty sweet, but I also
>> think that D should apply Koenig's rules: If a template is
>> dependent on a parameter from a particular module (say "a"), 
>> then
>> the functions inside that module should also be taken into
>> account when building the template instance. It would be
>> *partial* hijack in the sense that you can only hijack the
>> templates that operate on *your* types anyways, but in no case
>> change the behavior of that of others'.
>
> That still makes hijacking a real issue though. It limits the 
> hijacking to template functions, but it's still there.
>
> What if ADL was only used with UFCS on an instance of a 
> template type?
>
> e.g.
>
> void foo(T)(T t)
> {
>     import std.stdio;
>     writeln(t); // always std.stdio
>     t.writeln(); // used ADL, maybe std.stdio, but can be 
> specialised by T
>     "foo".writeln(); // always std.stdio
> }
>
> This kind of makes sense, because T could implement writeln() 
> as a member function anyway, so by using UFCS you are 
> voluntarily opening yourself up to hijacking.


That sounds relativelly safe. An even safer approach might be: 
Allow an ADL call only if there are no local symbols that match.

This way, you could argue that there is no hijacking, if there 
was no matching call.


More information about the Digitalmars-d mailing list