package ocaml-variants

  1. Overview
  2. No Docs

Description

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: 28 Mar 2018

Dependencies (5)

  1. base-ocamlbuild post
  2. base-threads post
  3. base-bigarray post
  4. base-unix post
  5. ocaml = "4.02.3" & post

Dev Dependencies

None

Used by (3)

  1. ocaml = "4.02.3"
  2. ocaml-config = "1" | >= "3"
  3. ocaml-options-only-no-flat-float-array < "1+bytecode-only"

Conflicts

None