I’m using the Core library in a project, and recently when I upgraded my OCaml from 4.08.1 to 4.10.0, plenty of compilation errors suddenly appear for comparison expressions like: if (xs = []) then ... or if (x = true) then ...
I saw that this change was discussed in this thread about monomorphic comparison operators in Base, but did not expect that Core would make it a default behavior.
So I’d like to ask since which version that Core removed such polymorphic comparison operators?
(I couldn’t find it in release notes of Core)
Also, if I define a sum type like type ternary = True | False | Unkn, what will be a correct way to write if (x = True) then ... (which is allowed in the new Core)?
I can temporarily fix by writing if (x == True) then ..., but using == doesn’t seem correct, since == is about comparing physical objects…
Thanks for spending your time to check my question.
The change was announced in [ANN] v0.13 release of Jane Street packages, although unfortunately it doesn’t look like the CHANGES.md file was updated in the repo. I would consider the thread to be the canonical announcement.
Here’s a few suggestions:
Define equality/compare functions using ppx_compare
type ternary = True | False | Unkn [@@deriving equal]
let f x = if (equal_ternary x True) then ...
Define equality/compare functions manually
let equal_ternary t1 t2 =
match t1, t2 with
| True, True | False, False | Unkn, Unkn -> true
| _ -> false
Explicitly request polymorphic comparison operators using the Poly module:
That depends on which preprocessor you are using. [@@deriving equal] comes from ppx_compare, whereas [@@deriving eq] comes from ppx_deriving. Base/Core and the like have better support for the former, which is a Jane Street project, although you can feel free to use the latter—the naming conventions are different, so it may not be as convenient.
Actually, I see now that [@@deriving eq] defines a function called equal_ternary, so I guess the names do, in fact, match, so they would probably be interchangeable.