Mal*_*Dew 2 ocaml pattern-matching
如何对string option refOCaml中具有类型的变量进行模式匹配。我需要提取这个变量的字符串部分,但我无法让它工作。
Aref只是具有可变字段的记录类型,称为contents:
type 'a ref = { mutable contents: 'a }
Run Code Online (Sandbox Code Playgroud)
因此,您可以像任何其他记录一样对其进行模式匹配:
match foo with
| { contents = Some str } -> str
| { contents = None } -> ...
Run Code Online (Sandbox Code Playgroud)
虽然我更喜欢打开第ref一个而不是匹配它:
match !foo with
| Some str -> str
| None -> ...
Run Code Online (Sandbox Code Playgroud)