2
votes

Let's say I define a type in OCaml this way:

type 'a foo = My_none | Bar of 'a;;

When making

let a = Bar 4;;

The Bar constructor is "called".

In the following function, does the matching call the constructor, or simply "recognize" the pattern without calling the constructor?

let get_bar x = match x with
     | My_none -> failwith "None"
     | Bar z -> z;;
1

1 Answers

5
votes

does the matching call the constructor, or simply "recognize" the pattern without calling the constructor?

The latter. Matching against Bar z does not create a new Bar value. It simply checks whether x is a Bar value.