Throwing Invalid_argument in Z.of_string, Q.of_string, and related functions seems to be an unfortunate API choice.
In the OCaml manual, Invalid_argument is an exception that is not meant to be caught:
exception Invalid_argument of string
Exception raised by library functions to signal that the given arguments do not make sense. The string gives some information to the programmer. As a general rule, this exception should not be caught, it denotes a programming error and the code should be modified not to trigger it.
Compare float_of_string, which consequently raises a Failure exception on invalid input, which is an exception meant to be thrown and caught under normal operations.
As there is no Q.of_string_opt or Q.is_valid_string function or similar, a user either has to implement their own code to check string validity or has to catch Invalid_argument, which is generally not intended to be done.
It would likely be more idiomatic to throw Failure instead of Invalid_argument on wrong (user) input. See also this discussion/post on discuss.ocaml.org on a different subject.
Maybe it is worth investigating whether catching Invalid_argument should be considered deprecated, and users of the zarith library could be advised to catch both Invalid_argument and Failure to allow for a transition in a future version of the API.
Throwing
Invalid_argumentinZ.of_string,Q.of_string, and related functions seems to be an unfortunate API choice.In the OCaml manual,
Invalid_argumentis an exception that is not meant to be caught:Compare
float_of_string, which consequently raises aFailureexception on invalid input, which is an exception meant to be thrown and caught under normal operations.As there is no
Q.of_string_optorQ.is_valid_stringfunction or similar, a user either has to implement their own code to check string validity or has to catchInvalid_argument, which is generally not intended to be done.It would likely be more idiomatic to throw
Failureinstead ofInvalid_argumenton wrong (user) input. See also this discussion/post ondiscuss.ocaml.orgon a different subject.Maybe it is worth investigating whether catching
Invalid_argumentshould be considered deprecated, and users of thezarithlibrary could be advised to catch bothInvalid_argumentandFailureto allow for a transition in a future version of the API.