local import hijacking

Daniel Kozak via Digitalmars-d digitalmars-d at puremagic.com
Thu Jan 14 07:54:02 PST 2016


V Thu, 14 Jan 2016 16:17:41 +0100
anonymous via Digitalmars-d <digitalmars-d at puremagic.com> napsáno:

> On 14.01.2016 15:25, Byron Heads wrote:
> > I got burned by this yesterday, this code should not compile
> >
> > import std.experimental.logger : trace;
> >
> >
> > void foo() {
> >      import std.net.curl : trace;
> >      trace("hello");
> > }
> >
> >
> > void main() {
> >      foo();
> > }  
> 
> I don't see a problem with that specific code. You're explicitly 
> importing `trace` from std.net.curl, so it can't be surprising that
> it's called.
> 
> But change one little detail and this qualifies as hijacking, I think:
> 
> ----
> void foo() {
>      import std.net.curl; /* not mentioning trace */
>      trace("hello");
> }
> ----
> 
> Imagine that std.net.curl didn't have a `trace` function when the
> code was written. std.experimental.logger.trace would have been
> called then. When a `trace` function is then added to std.net.curl,
> the code suddenly calls a different `trace`, without any warning.
> Hijacking.

Using local imports is dangerous. It should be used only with
selective imports



More information about the Digitalmars-d mailing list