2

I use vscode, with extensions of "OCaml and Reason IDE"

Here is my result in utop:

utop # 1. = 1. ;;    
Line 1, characters 0-2:
Error: This expression has type float but an expression was expected of type
             int

And also for String:

utop # "Me" = "Me";;
Line 1, characters 0-4:
Error: This expression has type string but an expression was expected of type
         int

Same for anything but int:

utop # 2 = 2 ;;
- : bool = true

">" "<" also have the same symptom. I don't know what actually happens. Can anyone help me out ? Thanks a lot!

1 Answers1

7

You are probably using JaneStreet Base library. Maybe you imported it like that:

open Base;;

Base tries to limit exceptions to functions that have explicit _exn suffix, so it shadows the built-in polymorphic equality (=) which can raise an exception on some inputs (for example, if you compare structures containing functions).

You can get polymorphic equality back as follows:

let (=) = Poly.(=);;

Or you can use it with a local import: Poly.(x = y).

There are pros and cons to polymorphic comparison. The consensus seems to be that using monomorphic comparison (for example, String.equal, etc) is a more robust choice, even though it is less convenient.

Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
  • 2
    Just as a side-note, it is not that polymorphic comparison is unsafe in the sense that it can crash your program, or break type system, or somehow make your program weaker. It is just less rigidly typed and a little bit less performant than its concrete counterpart and authors of the Janestreet libraries decided not to use it. – ivg Apr 13 '20 at 11:48
  • 1
    Thank you for your help. I tried "opam remove Base" eventually. My problem is that I set up OCaml with Base automatically installed. – Carvendish JANG Apr 14 '20 at 11:38