Taken from #1167
The below code returns "true" in OCaml
but returns "false" in bucklescript
if [2;6;1;1;2;1;4;2;1] < [2;6;1;1;2;1;4;2;1;409] then
print_string "true"
else
print_string "false"
So what is the semantic here? I don't really understand why [2;6;1;1;2;1;4;2;1] < [2;6;1;1;2;1;4;2;1;409] is true.
The length is shorter, and a 'none' value is less than a '409' value.
Yeah it is weird, I really do not like OCaml's polymorphic compare.
(WhereTF are Implicit Modules already? They could fix this in OCaml...)
That's another thing I was wondering, so what is a '409' value?
Just a random value added on to the length of the list.
But yeah, I'm pretty sure OCaml compares list length first before comparing contents, like 80% sure?
No. our implementation compares length first, the ocaml implementation does not...
No. our implementation compares length first, the ocaml implementation does not...
Ah, oh wait, I was reading it backwards! ^.^;
Wtf is OCaml's doing?!
_/me still hates OCaml's polymorphic compare, need implicit modules already..._
Sorry I got confused here, isn't it because OCaml compares the list lengths first the reason why it thinks [2;6;1;1;2;1;4;2;1] < [2;6;1;1;2;1;4;2;1;409] is true?
@dorafmon ah, it is a bug in https://github.com/bloomberg/bucklescript/blob/f8271f7fbcbb0b4eba8087617c6aa63022339b25/jscomp/runtime/caml_obj.ml#L157
when typeof a = 'number' we can not assume typeof b = 'number'
here in js 0 < [409,0] returns false, Obj.magic is dangerous even for experienced programmers : )
So could you explain a bit more? I still don't quite understand what is the polymorphic comparison doing here.
polymorphic comparison is unsafe comparison, its type signature is 'a -> 'a -> int, depending on runtime, the runtime will walk through each value and do a comparison.
Here the old implementation is buggy since it assumes that if the lhs is number, then the lhs is number which is wrong, since both number and variants like None will be compiled into number
Implicit modules would fix this. ;-)