ocaml中的匹配是否调用构造函数?
Does matching in ocaml call the constructor?
假设我在 OCaml 中这样定义一个类型:
type 'a foo = My_none | Bar of 'a;;
制作时
let a = Bar 4;;
Bar
构造函数是 "called"。
在下面的函数中,匹配是调用构造函数,还是直接"recognize"模式不调用构造函数?
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?
后者。匹配 Bar z
不会创建新的 Bar
值。它只是检查 x
是否是 Bar
值。
假设我在 OCaml 中这样定义一个类型:
type 'a foo = My_none | Bar of 'a;;
制作时
let a = Bar 4;;
Bar
构造函数是 "called"。
在下面的函数中,匹配是调用构造函数,还是直接"recognize"模式不调用构造函数?
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?
后者。匹配 Bar z
不会创建新的 Bar
值。它只是检查 x
是否是 Bar
值。