有没有办法将构造函数作为函数传递?
type foo =
| Foo of int
| Bar of int
let foo x = Foo x
let bar = fun x -> Bar x
是否有任何函数的简写 foo
和 bar
? 我想把一个构造函数作为函数传入,但写起来似乎很笨重 fun x -> Bar x
.
.增加 后缀在类型定义处,如。with variants
然后用Fieldslib的语法扩展编译它。它自动生成
type foo = Foo of int | Bar of int with variants;;
和
type foo = Foo of int | Bar of int
val bar : int -> foo = <fun>
val foo : int -> foo = <fun>
module Variants :
sig
val bar : (int -> foo) Variantslib.Variant.t
val foo : (int -> foo) Variantslib.Variant.t
end
为你。