Reputation: 1057
Say I have a variant v defined by:
type value =
| Value of int
| Error of string;;
I want to do something if v is a Value and something else if v is an Error, how can I determine this and perform different behaviors based on it?
Upvotes: 0
Views: 455
Reputation: 66808
That's what the match
expression is for:
match v with
| Value n -> (* Something with n *)
| Error s -> (* Something with s *)
(Insofar as OCaml is a functional language, it might be better to think in terms of values rather than behaviors. But OCaml can also be an imperative language if you wish.)
Upvotes: 2