ocaml-variantsversion
4.02.3+curried-constr

4.02.3+curried-constr

Variant constructors as functions

Suppose we have:

type t = Foo of int * float

Then

Foo

is equal to fun (x,y) -> Foo (x,y). And,

(Foo ..)        (* This is not valid in the vanilla OCaml *)

and

!Foo            (* If you keep the vanilla syntax *)

are equal to fun x y -> Foo (x,y).

It works for list cons constructor too:

(::)    : ('a * 'a list) -> 'a list
(:: ..) : 'a -> 'a list -> 'a list
!(::)   : 'a -> 'a list -> 'a list

Polymorphic variants as functions

(`Foo ..)         (* This is not valid in the vanilla OCaml *)
!`Foo

are equivalent to

fun x -> `Foo x

Note that (`Foo ..) always take only one argument: the arity of the polymorphic variant constructors is at most one and it is determined purely syntactically.

(`Foo..) (1,2,3)  (* `Foo (1,2,3) *)
(`Foo..) 1 2 3    (* (`Foo 1) 2 3  which ends in a type error *)

Code (`Foo) has no special meaning. It is just equivalent to `Foo.

Samples

You can try examples at testsuite/curried_constr/test.ml.

AuthorsXavier Leroy, Damien Doligez, Alain Frisch, Jacques Garrigue, Didier Rémy and Jérôme Vouillon
LicenseLGPL-2.1-or-later WITH OCaml-LGPL-linking-exception
Published
Homepagehttps://ocaml.org
Issue Trackerhttps://github.com/ocaml/opam-repository/issues
MaintainerDavid Allsopp <david@tarides.com>
Available!(os = "macos" & arch = "arm64")
Dependencies
Source [http] https://github.com/camlspotter/ocaml/archive/4.02.3+curried-constr.tar.gz
sha256=53f5420416e6f9dbd8abb790dd4d2e1cf62c04e042a320a02086c4cdc5a1cd48
md5=754cb6ef120d90273610ab5f860b99e7
Edithttps://github.com/ocaml/opam-repository/tree/master/packages/ocaml-variants/ocaml-variants.4.02.3+curried-constr/opam