Up

Module Tag

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

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

Signature

type ('variant, 'args) create =
| Args of 'args -> 'variant
| 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: | A of int "A" | `a of int "a" | `A of 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 2: | A of int * float etc.

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 *)

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 *)

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

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

val tyid : ('a, 'args) t -> 'args Typerep_lib.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