[Issue 19686] New: sgn is too greedy

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Feb 19 08:24:33 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=19686

          Issue ID: 19686
           Summary: sgn is too greedy
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: simen.kjaras at gmail.com

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

--


More information about the Digitalmars-d-bugs mailing list