Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric conversion functions: do function raises when target type is smaller than source type? #4

Open
braibant opened this issue Apr 4, 2016 · 3 comments

Comments

@braibant
Copy link

braibant commented Apr 4, 2016

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.

@mwweissmann
Copy link
Collaborator

the semantic is similar to the one in C -- there will be no exceptions raised, just truncated values

@keleshev
Copy link

keleshev commented May 4, 2016

I wish one could select different behavior. For example:

  • Int24.of_int32 or Int24.Wrapping.of_int32—truncates
  • Int24.of_int32_exn or Int24.Exn.of_int32—raises exception
  • Int24.of_int32_opt or Int24.of_int32—returns None on wrapping.

@mwweissmann what do you think of that feature and its API?

@mwweissmann
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants