package ppx_variants_conv
Build not checkedInstall
dune-project
Dependency
Authors
Maintainers
Sources
sha256=66c9c4499386b1672e7b5363bd7d75a5db68caf42c094758bbe258351eef124f
md5=5b10fe0375d7d3276864f711c744b500
Description
Part of the Jane Street's PPX rewriters collection.
Added to opam-repository:
README
ppx_variants_conv
Generation of accessor and iteration functions for ocaml variant types.
ppx_variants_conv is a ppx rewriter that can be used to define first class values representing variant constructors, and additional routines to fold, iterate and map over all constructors of a variant type.
It provides corresponding functionality for variant types as ppx_fields_conv provides for record types.
Basic use of [@@deriving variants] and variantslib
This code:
type 'a t =
| A of 'a
| B of char
| C
| D of int * int
[@@deriving variants]generates the following values:
(** first-class constructor functions *)
val a : 'a -> 'a t
val b : char -> 'a t
val c : 'a t
val d : int -> int -> 'a t
(** higher order variants and functions over all variants *)
module Variants : sig
val a : ('a -> 'a t) Variant.t
val b : (char -> 'a t) Variant.t
val c : ('a t) Variant.t
val d : (int -> int -> 'a t) Variant.t
val fold :
init: 'b
-> a:('b -> ('a -> 'a t) Variant.t -> 'c)
-> b:('c -> (char -> 'a t) Variant.t -> 'd)
-> c:('d -> ('a t) Variant.t -> 'e)
-> d:('e -> (int -> int -> 'a t) Variant.t -> 'f)
-> 'f
val iter :
a: (('a -> 'a t) Variant.t -> unit)
-> b: ((char -> 'a t) Variant.t -> unit)
-> c: (('a t) Variant.t -> unit)
-> d: ((int -> int -> 'a t) Variant.t -> unit)
-> unit
val map :
'a t
-> a: (('a -> 'a t) Variant.t -> 'a -> 'r)
-> b: ((char -> 'a t) Variant.t -> char -> 'r)
-> c: (('a t) Variant.t -> 'r)
-> d: ((int -> int -> 'a t) Variant.t -> int -> int -> 'a t -> 'r)
-> 'r
val make_matcher :
a:(('a -> 'a t) Variant.t -> 'b -> ('c -> 'd) * 'e)
-> b:((char -> 'f t) Variant.t -> 'e -> (char -> 'd) * 'g)
-> c:('h t Variant.t -> 'g -> (unit -> 'd) * 'i)
-> d:((int -> int -> 'j t) Variant.t -> 'i -> (int -> int -> 'd) * 'k)
-> 'b
-> ('c t -> 'd) * 'k
val to_rank : _ t -> int
val to_name : _ t -> string
(** name * number of arguments, ie [("A", 1); ("B", 1); ("C", 0); ("D", 2)]. *)
val descriptions : (string * int) list
endVariant.t is defined in Variantslib as follows:
module Variant = struct
type 'constructor t = {
name : string;
(* the position of the constructor in the type definition, starting from 0 *)
rank : int;
constructor : 'constructor
}
endThe fold, iter, and map functions are useful in dealing with the totality of variants. For example, to get a list of all variants when all the constructors are nullary:
type t =
| First
| Second
| Third
[@@deriving variants]let all =
let add acc var = var.Variantslib.Variant.constructor :: acc in
Variants.fold ~init:[]
~first:add
~second:add
~third:addJust like with [@@deriving fields], if the type changes, the compiler will complain until this definition is updated as well.
ppx_variant_libs works similarly on simple polymorphic variants (without row variables and without inclusion).
Dependencies (5)
-
ppxlib
>= "0.11.0" & < "0.14.0" -
dune
>= "2.0.0" -
variantslib
>= "v0.14" & < "v0.15" -
base
>= "v0.14" & < "v0.15" -
ocaml
>= "4.04.2"
Dev Dependencies
None
Optional Dependencies
None
Used by (8)
Conflicts
None
Build status
Results of building ppx_variants_conv with each compiler on check.ci.ocaml.org, run of against opam-repository 78c202b. All results for this package.
Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.
This version is not in the latest run; the versions below are.
| Version | OCaml 4.11 | OCaml 4.14 | OCaml 5.2 | OCaml 5.4 | OCaml 5.5 |
|---|---|---|---|---|---|
| v0.17.1 | n/a | n/a | good | good | good |
| v0.16.0 | n/a | good | good | n/a | n/a |
| v0.15.0 | good | good | good | n/a | n/a |
- good Package successfully built
- partial One of the dependencies failed to build
- bad Package failed to build
- n/a Package is not available in this environment
- internal Internal failure