On Wednesday, 18 September 2019 at 13:24:05 UTC, berni wrote:
On Wednesday, 18 September 2019 at 12:37:28 UTC, Simen Kjærås wrote:
How to resolve this, though? The simplest solution is to not use selective imports:

    import std.math;
    import std.complex;

    writeln(abs(complex(1.0,1.0)));
    writeln(abs(1.0));

That works. But: I'm trying to write some code for math.d and when I put this code inside math.d it doesn't work anymore. Also removing "import std.math" or moving it after the other import, did not help.

So what you have is basically this?

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));
}

That does indeed fail to compile, and there's no easy way to introduce the module-level abs() function to the scope. Again though, MergeOverloads to the rescue:

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;
}

--
  Simen

Reply via email to