当OCaml变量以`#`开头时,它是什么意思?(哈希标志/英镑符号)?

Max*_*ber 5 ocaml types functional-programming pattern-matching

我在模式匹配中对此进行了调查

| {call_name = #bundle_source; _ }

资源

在代码的早期,bundle_source定义为type(type bundle_source = ...).

那么哈希标志是什么意思呢?{call_name = #bundle_source }在模式匹配中是否意味着call_name预期值具有类型bundle_source

我搜索了手册中的"哈希标志"和"英镑标志",但一无所获.

Jef*_*eld 11

这是构造匹配多态变体值集合的模式的简写.

该文档在OCaml手册的6.6节中:

如果[('a,'b,…)] typeconstr = [ ` tag-name1 typexpr1 | … | ` tag-namen typexprn]定义了类型,则模式#typeconstr是以下or模式的简写:( `tag-name1(_ : typexpr1) | … | ` tag-namen(_ : typexprn)).它匹配所有类型的值[< typeconstr ].

# type b = [`A | `B];;
type b = [ `A | `B ]
# let f x =
  match x with
  | #b -> "yes"
  | _ -> "no";;
val f : [> b ] -> string = <fun>
# f `A;;
- : string = "yes"
# f `Z;;
- : string = "no"
Run Code Online (Sandbox Code Playgroud)

(我也不熟悉这种表示法.)