package type_id

  1. Overview
  2. Docs
Type identifiers, useful for runtime type-safe casting/coersions

Install

Dune Dependency

Authors

Maintainers

Sources

type_id-0.0.1.tbz
sha256=8665400fdbe05351cb9c1eddd535f5ed5f8095fe595e6bd081dda72c79cb8439
sha512=08bb56914a3d3137503efb1254cc51340de107e150f0ad2af102f32744a4837c4630fca2f75a4cb3deaae62b67701f06db9cfb80a541f7bb88d37c4adc5fc775

Description

Published: 12 Feb 2024

README

type_id

What does this library do?

type_id provides a type -- Type_id.t -- which can be used to reference and identify a type. This can then be used to construct type proofs between two types which share the same type ID, extending the usefulness of the type_eq library.

module Types : sig
  (* These types are equal, but they're also opaque. *)
  type a
  type b

  (* This type is not equivalent to the above types. *)
  type c

  val a_of_int: int -> a
  val b_of_int: int -> b
  val c_of_int: int -> c

  val a_id : a Type_id.t
  val b_id : b Type_id.t
  val b_id_2 : b Type_id.t
  val c_id : c Type_id.t
end = struct
  type a = string
  type b = string
  type c = int

  let a_of_int = string_of_int
  let b_of_int = string_of_int
  let c_of_int x = x

  let a_id = Type_id.make ~label:"string"
  let b_id = a_id
  let b_id_2 = Type_id.make ~label:"string-2"
  let c_id = Type_id.make ~label:"int"
end

(* These types are different... *)
let a: Types.a = Types.a_of_int 123
let b: Types.b = Types.b_of_int 456

(* But, we can cast between them using their type IDs: *)
let a': Types.a =
  match Type_id.eq_res Types.a_id Types.b_id with
    | Error e -> failwith (* ... *)
    | Ok eq_a_b -> Type_eq.coerce eq_a_b b

Why is this library useful?

Sometimes you'll be working with types which are opaque -- as in the example above. This utility can be used to determine if two opaque types are indeed equal, which can allow you to cast between them, among other operations.

See the .mli file for a fully-documented interface.

Note that a stripped-down version of this utility is included as Type.eq in OCaml 5+: https://github.com/ocaml/ocaml/blob/c2d00ef67b4af1e6ba90e77e4106770bbdd88a01/stdlib/type.ml#L18

Dependencies (3)

  1. type_eq >= "0.0.1"
  2. dune >= "3.11" & >= "3.1"
  3. ocaml >= "4.0.3"

Dev Dependencies (2)

  1. alcotest with-test
  2. odoc with-doc

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.