Problem with using std.math: abs and std.complex: abs at the same time
Simen Kjærås
simen.kjaras at gmail.com
Thu Sep 19 07:26:17 UTC 2019
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
More information about the Digitalmars-d-learn
mailing list