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.

Published
Maintainerplatform@lists.ocaml.org
Dependencies
Conflicts
Source [http] https://github.com/camlspotter/ocaml/archive/4.02.3+curried-constr.tar.gz
md5=754cb6ef120d90273610ab5f860b99e7
StatisticsNot installed in the last month.
Edithttps://github.com/ocaml/opam-repository/tree/master/packages/ocaml-variants/ocaml-variants.4.02.3+curried-constr/opam
Required by