ocaml / Zarith

The Zarith library implements arithmetic and logical operations over arbitrary-precision integers and rational numbers. The implementation, based on GMP, is very efficient.
Other
226 stars 69 forks source link

Suggestion: having _opt version without exceptions #110

Closed clarus closed 2 years ago

clarus commented 2 years ago

Hello,

Many of the ZArith primitives can raise an exception (division per zero, overflow when converting to int, ...) and this can be hard to track sometimes. I think it would be cool to have _opt versions returning an option type instead, as for the standard library of OCaml with List.find and List.find_opt for example. I can contribute if needed.

Thanks!

antoinemine commented 2 years ago

I guess we could. In all cases, I'd rather not do it at the C level. It's easy to add such functions as OCaml wrappers, but then it could be done on the client side as well, no need to clutter the library. Which exceptions are raised by which functions is well documented, so I don't get why it can be hard to track sometimes, and where the need comes from. The API is much closer to Int64 (which has very few option types) than List, so the comparison is not very apt.

clarus commented 2 years ago

OK, yes it can be done client-side. Closing then. To me, the main interest would be to encourage people to write exception-free code with explicit error handling, when this is needed.