Consider the following class:
class user =
object (self)
method id : 'ab. ([< `A | `B ] as 'ab) -> 'ab = fun x -> x
method act : [ `A | `B ] -> string =
fun x -> match self#id x with `B -> "B" | `A -> "A"
end
Calling
(new user)#act `A
yields, surprisingly, a result of "B"
. However, any of the following changes cause it to yield "A"
instead:
- Changing the type of
id
to'ab. 'ab -> 'ab
. - Changing the type of
id
to[ `A | `B ] -> [ `A | `B ]
. - Matching
(self#id x :> [ `A | `B ])
instead. - Changing the order of the match clauses.
In addition:
- Calling
(new user)#act `B
instead also yields a return value of"B"
. - Adding a catchall case
| _ -> "C"
to the match (with or without changing the type ofact
to[> `A | `B ] -> string
) changes the return value to"C"
.
Is something going on here that makes sense?