[Issue 21500] public import in mixin template in module a fails when module b imports a.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Feb 1 16:42:32 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=21500

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305 at gmail.com

--- Comment #2 from RazvanN <razvan.nitu1305 at gmail.com> ---
Yes, it is supposed to fail. Is is by design that imports inside mixins are
visible only in the scope of the mixin. The explanation for that is that you do
not want to risk hijacking symbols. Take for example:

mixin template baz()
{
    import std.stdio : write;
    void func() { /* use write */}
}

void write
{
    /* log something */
}

struct B
{
    mixin baz;
    void foo()
    {
         write();   // oops, calls std.stdio.write
    }

}

Imagine that baz is written in a library and you don't know exactly what
imports it is using, so you expect that your user-defined function is called,
however, the symbol is hijacked by the import inside the mixin. This is
prevented by the current behavior where imports inside mixins are only visibile
in the scope of the mixin.

The current behavior is the correct one, so closing this as invalid.

--


More information about the Digitalmars-d-bugs mailing list