Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorthand for constructor function

Tags:

ocaml

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.

like image 791
Nat Mote Avatar asked Apr 17 '14 07:04

Nat Mote


Video Answer


1 Answers

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
like image 82
Virgile Avatar answered Sep 20 '22 20:09

Virgile