Module Core_kernel__.Type_immediacy
Converting between values and ints
Consider an arbitrary type 'a for which you have built a type-immediacy witness using this interface. Let's call it w : 'a t.
You can use the two following functions and w to cast back and forth values from the type 'a to the type int:
val int_as_value : 'a t -> int -> 'a option
val value_as_int : 'a t -> 'a -> int optionFor the rest of this section, we will assume int_as_value and value_as_int partially applied to w.
Consider the following cases:
let
vbe an immediate value.Let
ibe theintthat internally representsv. Then,value_as_int vreturnsSome i.We can also recover
vby using the conversions that go the other way. In particular,int_as_value ireturnsSome v.
let
vbe a boxed value that cannot be converted to anint.value_as_int vreturnsNonebecause there does not exist an int s.t.int_as_value ievaluates toSome v.
let
ibe an int that does not represent any value of type'aint_as_value ireturnsNone.
Faster *_exn functions and functions with boolean results
value_is_int v is a faster equivalent to Option.is_some (value_as_int v). value_as_int_exn v is a faster equivalent to Option.value_exn (value_as_int v).
int_is_value i is a faster equivalent to Option.is_some (int_as_value i). int_as_value_exn i is a faster equivalent to Option.value_exn (int_as_value i).
These are lightweight functions that avoid allocating the option. value_is_int (resp int_is_value) can be used with value_as_int_exn (resp int_as_value_exn) to avoid both allocation or using a try with statement, paying only some small amount of CPU time for calling value_is_int (resp int_is_value):
match value_as_int v with
| Some v -> some v
| None -> noneVS
if value_is_int v
then some (value_as_int_exn v)
else noneExample
Consider the following type:
type test =
| A
| B
| C of int
with typerepType test is sometimes immediate, as A is represented as 0, B as 1, and C is a boxed value. We can construct a witness of type test Sometimes.t by using Sometimes.of_typerep or of_typerep and extracting the witness. Let's call the witness w here. We can now use it to safely convert between values of test and int:
Sometimes.value_as_int w A evaluates to Some 0 Sometimes.value_as_int w B evaluates to Some 1 Sometimes.value_as_int w (C 1) evaluates to None
Sometimes.int_as_value w 0 evaluates to Some A Sometimes.int_as_value w 1 evaluates to Some B Sometimes.int_as_value w n evaluates to None for all other values n
Consider this other example:
type test = bool with typerepType test is always immediate, since true is represented as 1 and false as 0. We can construct a witness of type test Always.t by using Always.of_typerep or of_typerep and extracting the witness. Let's call the witness w:
Always.value_as_int w false evaluates to Some 0 Always.value_as_int w true evaluates to Some 1
Always.value_as_int_exn w false evaluates to 0 Always.value_as_int_exn w true evaluates to 1
Always.int_as_value w 0 evaluates to Some false Always.int_as_value w 1 evaluates to Some true Always.int_as_value w (-1) evaluates to None
Always.int_as_value_exn w 0 evaluates to false Always.int_as_value_exn w 1 evaluates to true Always.int_as_value_exn w (-1) raises
N-ary types that are immediate independently of their type arguments
We also provide For_all_parameters_S* functors. Those are useful when one has a type with type parameters, but knows that values of that type will always be immediate (for example) no matter what the actual parameter is. They can use Always.For_all_parameters_S* to obtain access to a polymorphic witness.
An exception is raised on functor application if such witness cannot be obtained. That happens either because the witness depends on the actual type parameter, or because the type has a different witness (e.g. Sometimes instead of Always).
module Always : sig ... endmodule Sometimes : sig ... endmodule Never : sig ... endval of_typerep : 'a Core_kernel__.Import.Typerep.t -> 'a t
type 'a dest=|Always of 'a Always.t|Sometimes of 'a Sometimes.t|Never of 'a Never.t
val dest : 'a t -> 'a destval int_as_value : 'a t -> Core_kernel__.Import.int -> 'a Core_kernel__.Import.optionval int_as_value_exn : 'a t -> Core_kernel__.Import.int -> 'aval int_is_value : 'a t -> Core_kernel__.Import.int -> Core_kernel__.Import.boolval value_as_int : 'a t -> 'a -> Core_kernel__.Import.int Core_kernel__.Import.optionval value_as_int_exn : 'a t -> 'a -> Core_kernel__.Import.intval value_is_int : 'a t -> 'a -> Core_kernel__.Import.bool