package typerep

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Witness of a tag, that is an item in a variant type, also called an "applied variant Constructor"

The first parameter is the variant type, the second is the type of the tag parameters. Example:

type t =
  | A of (int * string)
  | B of string
  | C of { x : int; y : string }

this type has two constructors. for each of them we'll have a corresponding Tag.t

val tag_A : (t, (int * string)) Tag.t
val tag_B : (t, string        ) Tag.t
val tag_C : (t, (int * string)) Tag.t

Inline record in variant are typed as if their definition was using tuples, without the parenthesis. This is consistent with their runtime representation. But the distinction is carried and available for introspection as part of the Tag.t. See args_labels.

type (!'variant, !'args) create =
  1. | Args of 'args -> 'variant
  2. | Const of 'variant
type ('variant, 'args) t
val label : ('a, 'b) t -> string

The name of the constructor as it is given in the concrete syntax Examples:

         Constructor        | label
         -------------------------
         | A of int         |  "A"
         | `a of int        |  "a"
         | `A of int        |  "A"
         | A of { x : int } |  "A"

for standard variant, the ocaml syntax implies that this label will always starts with a capital letter. For polymorphic variants, this might be a lowercase char. For polymorphic variant, this label does not include the ` character.

val arity : ('a, 'b) t -> int

The size of the ocaml heap block containing the arguments

Examples:

          0: | A | 'A
          1: | A of int | `A of int | A of (int * int) | `A of (int * int)
             | `A of int * int
             | A of { x : int}
          2: | A of int * float
             | A of { x : int; y : string }
          etc.
val args_labels : ('a, 'b) t -> string list

The label of the fields for inline records. For other forms of tags, this is the empty list. When this returns a non empty list, the length of the returned list is equal to the arity.

Example:

         (1) Empty:

           | A | 'A
           | A of int | `A of int | A of (int * int) | `A of (int * int)
           | `A of int * int
           | A of int * float

         (2) Non empty:

           | A of { x : int }               -> [ "x" ]
           | A of { x : int; y : string }   -> [ "x" ; "y" ]
val index : ('a, 'b) t -> int

The index of the constructor in the list of all the variant type's constructors Examples:

type t =
  | A of int          (* 0 *)
  | B                 (* 1 *)
  | C of int          (* 2 *)
  | D of char         (* 3 *)
  | E of { x : int }  (* 4 *)
val ocaml_repr : ('a, 'b) t -> int

ocaml_repr is related to the runtime of objects. this is essentially a way of giving one the ability to rebuild dynamically an Obj.t representing a tag.

Polymorphic variants: ---------------------

ocaml_repr is the hash of the label, as done by the compiler. Example: print_int (Obj.magic `bar) (* 4895187 *) print_int (Obj.magic 'foo) (* 5097222 *)

Standards variants: -------------------

ocaml_repr is the tag corresponding to the constructor within the type. the way it works in the ocaml runtime is by partitioning the constructors regarding if they have some arguments or not, preserving the order, then assign increasing index withing each partition. Example:

type t =                  (* no arg *)  (* args *)
  | A                       (* 0 *)
  | B of int                              (* 0 *)
  | C                       (* 1 *)
  | D of (float * string)                 (* 1 *)
  | E                       (* 2 *)
  | F                       (* 3 *)
  | G of string                           (* 2 *)
  | H of { x : int }                      (* 3 *)
val create : ('variant, 'args) t -> ('variant, 'args) create

Give back a way of constructing a value of that constructor from its arguments.

Examples:

type t =
  | A of (int * string)
  | B of int * float
  | C
  | D of { x : int; y : string }

create will return something equivalent to: tag_A : Args (fun (d : (int * string) -> A d) tag_B : Args (fun (i, f) -> B (i, f)) tag_C : Const C tag_D : Args (fun (x, y) -> D { x; y })

val tyid : ('a, 'args) t -> 'args Typename.t

return the type_name of the arguments. might be used to perform some lookup based on it while building a computation for example

val traverse : ('a, 'args) t -> 'args t

get the representation/computation of the arguments

val internal_use_only : ('a, 'b) Tag_internal.t -> ('a, 'b) t