I have a question related to private type abbreviations
I'm interfacing C++ and ocaml and I want to make sure that the ranges of integer types are correct and reflect them in the ocaml interface.
So I define
type uint8 = private int
and
type int8 = private int
same for the other sizes/signedness
and the appropriate functions to do range checking (those are external and use
std::numeric limits)
external uint8 : int -> uint8 = "make_uint8"
...
this gives typesafety and avoids boxing/unboxing and makes sure that the user can
only pass values that are range checked at the earliest opportunity.
Now I wanna check my code
for all the types I wanna use 1 checking function something like this:
let test_conversions (the_fun : int -> 't) (the_val : int) =
try
let the_t = the_fun the_val in
Printf.printf "Numbers are %d\n" (the_t : 't :> int)
with
| Invalid_argument str -> Printf.printf "Error: %s" str
let () = test_conversions uint8 1 -> will work
..
let () = test_conversions uint64 (-1) -> will print Error...
Now this doesn't typecheck because the type var 't in the signature is too general.
what I need to put there is "a type coercible to int"
Is that possible? Is there some way to achieve this?
Thanks in advance,
Immanuel