Subject: | Enhancement: Policy to detect non-transitive comparison functions |
Comparison operator, if used with sort, should be transitive
http://en.wikipedia.org/wiki/Comparison_sort
Any complex comparison function need a proof of transitivity (maybe unit tests).
I am not sure how hard it will be to detect possibly non-transitive functions.
Maybe throw a warning if left part of '<=>'/'cmp' expression differs much from right part of it (and if <=>/cmp inside sort {} block) ?
for example
OK:
sort { norm($a) <=> norm($b) } @a
OK:
sort { $a <=> $b } @a
OK:
sort { ($a||999) <=> ($b||999) } @a
Not OK:
sort { ($a||999) <=> ($b//999) } @a
Not ok:
sort { $a->[0] && $b->[0] ? $a->[0] <=> $b->[0] : $a->[1] <=> $b->[1] } @a
OK(Ignore):
sort{ somefunc($a,$b) } @a
This often used and does not contain bugs, not sure how to detect that, however.
sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @a