I’m trying to understand if there’s a convenient way to use constructors
with the same name but different types.
For instance:
datatype CONSOV =
| acons of int
| acons of (CONSOV,CONSOV)
| bcons of int
val a1 = acons(5)
val a2 = acons(acons(1),acons(2))
val tst = case a1 of
| acons _ => print(“acons\n”)
| bcons X => print(“bcons\n”)
// | acons (,) => print(“acons2\n”) //This clause is redundant
The compiler warns us that not all cases are taken into account above, but
errors if the last line is uncommented.
Oddly, to me, “case a1” will fail with a run time error, but “case a2” will
run ok with the above code.
What I was hoping was that there would be a way to group several
constructors together (i.e. by name) to save on making many alternative
case expressions.
If we use named variables everything is fine:
val tst = case a1 of
| acons (x) => print(“acons\n”)
| bcons X => print(“bcons\n”)
| acons (x,y) => print(“acons2\n”) //This clause is redundant