Source

ocaml-dynatvar / dynvar.mli

Full commit
(** Dynamically typed variables

WARNING!  It's not tested yet!

Author: Dmitry Grebeniuk - gdsfh1 at gmail dot com

Look at http://janestcapital.com/?q=node/18 too
*)

(** Exception [Type_error (value_type, used_with_type)] is
    raised by [get] if you are trying to get the value of
    wrong type.
*)
exception Type_error of (string * string)
;

(** Type of the value that represents the type of dynamic variable *)
type ty 'a;

(** Type of dynamic variable *)
type var;

(** [mktype typename] creates a value that represents the type of
    dynamic variables you can create.  (i.e. it's the witness
    of dynamic variable's type)
 *)
value mktype : string -> ty 'a
;

(** [var ty val] creates dynamic variable with type [ty]
    and value [val].  The success of this operation is guaranteed
    by OCaml's type system.
*)
value var : ty 'a -> 'a -> var
;

(** [get ty var] gets the value of dynamic variable [var]
    which should be created by [var ty val] with the same [ty].
    (same == "physically equal").
    @raise Type_error when types differ.
*)
value get : ty 'a -> var -> 'a
;

type res 'a 'b = [= `Ok of 'a | `Error of 'b ]
;

(** [get_res ty var] returns either [`Ok val] with value of
    [var], or [`Error (value_type, used_with_type)] on type error.
*)
value get_res : ty 'a -> var -> res 'a (string * string)
;