D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21191 - min should be stable: when in doubt, return the first argument
Summary: min should be stable: when in doubt, return the first argument
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2020-08-23 01:28 UTC by Andrei Alexandrescu
Modified: 2020-08-28 06:19 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 Andrei Alexandrescu 2020-08-23 01:28:02 UTC
This should work:

@safe unittest
{
    import std.algorithm;
    assert(min(1.0, double.nan) == 1.0);
    assert(min(double.nan, 1.0) is double.nan);
    static struct A {
        int x;
        string y;
        int opCmp(A a) { return int(x > a.x) - int(x < a.x); }
    }
    assert(min(A(1, "first"), A(1, "second")) == A(1, "first"));
}

In other words, min should return its first argument unless it makes a positive determination that the second argument "deserves" to be returned instead.
Comment 1 Dlang Bot 2020-08-23 01:29:26 UTC
@andralex updated dlang/phobos pull request #7605 "min must be stable" fixing this issue:

- Fix Issue 21191 - min should be stable: when in doubt, return the first argument

https://github.com/dlang/phobos/pull/7605
Comment 2 Dlang Bot 2020-08-28 06:19:46 UTC
dlang/phobos pull request #7605 "min must be stable" was merged into master:

- beb200d546b2f15b8c9620c5c520fd6ba584fb79 by Andrei Alexandrescu:
  Fix Issue 21191 - min should be stable: when in doubt, return the first argument

https://github.com/dlang/phobos/pull/7605