Thread overview
[Issue 20226] selective import in function scope fails to merge overload sets
Sep 19, 2019
Simen Kjaeraas
Dec 17, 2022
Iain Buclaw
September 19, 2019
https://issues.dlang.org/show_bug.cgi?id=20226

--- Comment #1 from Simen Kjaeraas <simen.kjaras@gmail.com> ---
Note that even though changing the order of imports makes the above code
compile, that's because std.math's abs() implementation ends up being the one
available, and calling abs() on e.g. a Complex!float would fail in that case
(but would compile in the above example).

This same issue occurs when importing a function that also exists in a parent scope:

import std.stdio;

float abs(float f) {
    return f < 0 ? -f : f;
}

unittest {
    import std.complex : complex, abs;

    auto a = complex(1.0,1.0);
    auto b = 1.0f;

    writeln(abs(a));
    writeln(abs(b));
}


In a non-function scope, the solution is to use an alias:

float abs(float f) {
    return f < 0 ? -f : f;
}

struct S {
    import std.complex : complex, abs;
    alias abs = .abs;

    unittest {
        abs(1);
        abs(complex(1,1));
    }
}


However, this doesn't work inside functions:

float abs(float f) {
    return f < 0 ? -f : f;
}

unittest {
    import std.complex : complex, abs;
    alias abs = .abs; // declaration abs is already defined
}


A workaround exists in using a non-function scope to merge the overload sets:

float abs(float f) {
    return f < 0 ? -f : f;
}

unittest {
    import std.complex : complex, cabs = abs;
    alias abs = MergeOverloads!(cabs, .abs);
    abs(1);
    abs(complex(1,1));
}

template MergeOverloads(T...) {
    static foreach (E; T)
        alias MergeOverloads = E;
}

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=20226

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--
December 13
https://issues.dlang.org/show_bug.cgi?id=20226

--- Comment #2 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17923

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--