简体   繁体   中英

Does matching in ocaml call the constructor?

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;;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM