Issue 20478 - Phobos should have a trait to identify NaN-like behavior
Summary: Phobos should have a trait to identify NaN-like behavior
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P4 enhancement
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2020-01-03 20:53 UTC by Simen Kjaeraas
Modified: 2024-12-01 16:36 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Simen Kjaeraas 2020-01-03 20:53:53 UTC
Since we have the pattern of returning float.nan from opCmp to signal incomparable values, it makes sense to have a trait to identify this behavior. Something like this:

template hasNanComparison(T1, T2 = T1)
{
    static if (isFloatingPoint!T1)
        enum hasNanComparison = true;
    else static if (!isAggregateType!T1)
        enum hasNanComparison = false;
    else static if (!is(typeof((T1 a, T2 b) => a.opCmp(b))))
        enum hasNanComparison = false;
    else
        enum hasNanComparison = hasNanComparison!(ReturnType!((T1 a, T2 b) => a.opCmp(b)));
}

///
@safe unittest
{
    assert( hasNanComparison!float);
    assert( hasNanComparison!real);
    assert(!hasNanComparison!int);
    assert(!hasNanComparison!string);
    assert(!hasNanComparison!(int*));
    assert(!hasNanComparison!(int[]));

    struct S
    {
        float opCmp(S s)
        {
            return float.nan;
        }
    }
    assert( hasNanComparison!S);

    struct S2
    {
        S opCmp(S2 s)
        {
            return S();
        }
    }
    assert( hasNanComparison!S2);
}
Comment 1 Dlang Bot 2020-01-03 20:56:41 UTC
@Biotronic created dlang/phobos pull request #7342 "Fix issue 20478" fixing this issue:

- Fix issue 20478

https://github.com/dlang/phobos/pull/7342
Comment 2 dlangBugzillaToGithub 2024-12-01 16:36:07 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/10401

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB