Skip to content

Fallible conversion from string #170

Description

@JanBeh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions