Why is mostNegative!T not of type T?
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Sun Oct 17 20:21:21 UTC 2021
> Looking at the definition of clamp:
>
> auto clamp(T1, T2, T3)(T1 val, T2 lower, T3 upper)
> if (is(typeof(max(min(val, upper), lower))))
> in
> {
> import std.functional : greaterThan;
> assert(!lower.greaterThan(upper), "Lower can't be greater than
> upper.");
> }
> do
> {
> return max(min(val, upper), lower);
> }
>
> In turn, min and max use mostNegative and makes surprising deductions
> based on its type. The surprise has been deemed desirable and has become
> part of an unnecessarily complex mesh.
>
> The failing unittest is:
>
> int a = -5;
> uint f = 5;
> short b = 6;
> static assert(is(typeof(clamp(f, a, b)) == int));
>
> This is bizarre and very arguably a bug, as one would expect clamping a
> value of type `whatever` would yield a value of type `whatever`.
Update:
https://issues.dlang.org/show_bug.cgi?id=22414
https://github.com/dlang/phobos/pull/8293
More information about the Digitalmars-d
mailing list