Bug 19686 – sgn is too greedy

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2019-02-19T08:24:33Z
Last change time
2019-02-19T09:14:04Z
Keywords
pull
Assigned to
No Owner
Creator
Simen Kjaeraas

Comments

Comment #0 by simen.kjaras — 2019-02-19T08:24:33Z
The current implementation of sgn in Phobos is too greedy, and will match types that it doesn't work with. For reference, this is what sgn look like at time of writing: F sgn(F)(F x) @safe pure nothrow @nogc { // @@@TODO@@@: make this faster return x > 0 ? 1 : x < 0 ? -1 : x; } This greedyness leads to error messages with template spam when doing things like sgn("string"), and also makes it misbehave when used alongside other sgns for other types (e.g. std.complex). It should look more like this: F sgn(F)(F x) @safe pure nothrow @nogc if (isFloatingPoint!F || isIntegral!F) { // @@@TODO@@@: make this faster return x > 0 ? 1 : x < 0 ? -1 : x; }
Comment #1 by dlang-bot — 2019-02-19T08:31:39Z
@Biotronic created dlang/phobos pull request #6872 "Fix issue 19686 - sgn is too greedy" fixing this issue: - Fix issue 19686 - sgn is too greedy https://github.com/dlang/phobos/pull/6872
Comment #2 by dlang-bot — 2019-02-19T09:14:04Z
dlang/phobos pull request #6872 "Fix issue 19686 - sgn is too greedy" was merged into master: - 4cc23fd93a7f1741d319e634ca42b0bf0468509b by Simen Kjærås: Fix issue 19686 - sgn is too greedy https://github.com/dlang/phobos/pull/6872