Hi Grégoire, It's not directly possible in OCaml, but there are at least three methods for doing what you want.
The first one is to wrap your integers behind a constructor, e.g.
type user_id = User of int
type movie_id = Movie of int
let a = User 57 and b = Movie 80 in
if a = b then ...
This is the technique often used by Haskellites. Variant on the topic: use singleton records instead of singleton sums.
The second one is to make use of modules and abstract types, e.g.
module User =
struct
type id = int
let id_of_int x = x
...
end :
sig
type id (*Abstract type*)
val id_of_int : int -> id
end
(*same for b*)
let a = User.id_of_int 57 and b = User.id_of_int 80 in
if a = b then ...
This is probably the most common technique in OCaml, as it fits well with functorization.
Finally, you can use a phantom type, e.g.
type 'a id = {id: int} (*Type argument used only to differentiate between various kinds of ids*)
type user (*This type has no inhabitant, don't worry, it's only for coercions*)
type movie (*same here*)
let a = {id:57} : user id and b = {id:80}: movie id in
if a = b then ...
It's an elegant technique, which I personally like, but which can sometimes cause puzzling error messages if you forget coercions.
I hope this helps,
Regards,
David