Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether two values are created with the same constructor?

Tags:

ocaml

let's say I have

type t = A of int | B of int

let xx = A(2);;
let yy = A(3);;

and I want to test if the constructors of xx and yy are equal, is there an easy way to do this ? Instead of having to

match xx with
  A _ ->
  (match yy with A _ -> true | B _ -> false)
| B _ -> 
  (match yy with A _ -> false | B _ -> true);;

which gets quite messy when there many constructors on a type

like image 220
romerun Avatar asked Jun 29 '11 14:06

romerun


1 Answers

You can rewrite the above to, somewhat simpler:

match xx, yy with
| A _, A _
| B _, B _ -> true
| (A _ | B _), _ -> false

but I'm not aware of a solution without enumerating all the constructors.

like image 105
akoprowski Avatar answered Sep 27 '22 21:09

akoprowski