ocaml-stdint
ocaml-stdint copied to clipboard
Numeric conversion functions: do function raises when target type is smaller than source type?
Reading the "Numeric conversion functions" of the lib, it's not clear to me whether the functions there will raise if one tries to convert, e.g., an int32 into an int24, or if it will truncate/mask the value.
It could make sense to improve the doc to make this really clear.
the semantic is similar to the one in C -- there will be no exceptions raised, just truncated values
I wish one could select different behavior. For example:
-
Int24.of_int32
orInt24.Wrapping.of_int32
—truncates -
Int24.of_int32_exn
orInt24.Exn.of_int32
—raises exception -
Int24.of_int32_opt
orInt24.of_int32
—returnsNone
on wrapping.
@mwweissmann what do you think of that feature and its API?
This should be pretty easy to implement: Just check if the forward+backward conversion is idemnpotent: let f x = let y = of_int32 x in if Int32.of_int8 y = x then y else raise Overflow