[Issue 21109] Wrong result when using sort() on enum arrays
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Aug 7 08:14:18 UTC 2020
https://issues.dlang.org/show_bug.cgi?id=21109
--- Comment #6 from Andrej Mitrovic <andrej.mitrovich at gmail.com> ---
(In reply to Boris Carvajal from comment #5)
> There is no 'opCmp' defined for the struct so the runtime just use a memcmp
> for the comparison.
> The code itself calls '__cmp' from core/internal/array/comparison.d (because
> the element are an array '[S(2, [0, 0])]') and there the 3 'static if'
> conditions fail but if you define 'opCmp' the second path is taken.
>
> So the following works:
>
> struct S
> {
> int[] arr;
> int opCmp(ref const S s) const { return arr < s.arr; }
> }
>
> There is a comment on clone.d file that says:
>
> "Essentially, a struct which does not define opCmp is not comparable."
>
> This concise phrase should be on the spec.
Hmm.
In the past the compiler would also do memcmp when a struct didn't define
`opEquals`, but we changed it so it does member-by-member `==` comparison.
I wonder if we can do something similar for opCmp, or if that wouldn't make
sense..
--
More information about the Digitalmars-d-bugs
mailing list