Is there any way to pass a constructor as a function?
type foo =
| Foo of int
| Bar of int
let foo x = Foo x
let bar = fun x -> Bar x
Is there any shorthand for the functions foo
and bar
? I want to pass a constructor as a function, but it seems unwieldy to write fun x -> Bar x
.
camlspotter's answer was close enough, but in your case you want to use Variantslib and add with variants
at the end of your type definition:
type foo = Foo of int | Bar of int with variants;;
gives you the following:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With