Problem with using std.math: abs and std.complex: abs at the same time

Daniel Kozak kozzi11 at gmail.com
Wed Sep 18 12:14:06 UTC 2019


On Wed, Sep 18, 2019 at 2:05 PM berni via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> wrote:
>
> The following code doesn't compile:
>
> >import std.stdio;
> >
> >void main()
> >{
> >    import std.complex: abs, complex;
> >    import std.math: abs;
> >
> >    auto a = complex(1.0,1.0);
> >    auto b = 1.0;
> >
> >    writeln(abs(a));
> >    writeln(abs(b));
> >}
>
> The error message depends on the order of the two import
> statements. Seems like the second import is actually ignored.
>
> I hoped for a mechanism similar to overloading, which makes the
> compiler decide, which "abs" to use, depending on the type of the
> operand. Is there a way to do this? (As the code appears inside a
> template, something like std.math.abs() with static import
> doesn't work out well.)

import std.stdio;

void main()
{
    import std.complex: abs, complex;
    import std.math: mabs = abs;

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

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


More information about the Digitalmars-d-learn mailing list