Announcing convert 0.5.0

I am excited to announce version 0.5.0 of Convert, which can be found at https://github.com/NthPortal/convert. It is a small library designed to help facilitate conversions between types while allowing API clients to decide whether they want the conversion to throw exceptions or return Options.

The motivation behind this library is that there are legitimate uses both for returning Options and throwing exceptions, but supporting both behaviors would normally require duplicating conversion logic. For example, String.toInt (which is an alias for Java’s Integer.parseInt(String)) always throws exceptions, which is not desirable if you have lots of strings to parse, but you know a significant number of them do not represent valid Ints. Conversely, if you expect strings you need to parse to always represent valid Ints, then you do want the method to throw exceptions, because exceptions contain more information about what was wrong with the input (Option.get only tells you that it was invalid, but not what the input was or how it was invalid).

The library defines a typeclass Convert, with a type Result[T] for its conversion results. The two instances of Convert are Convert.Throwing (for which Result[T] = T), which throws exceptions when a conversion fails, and Convert.AsOption (for which Result[T] = Option[T]), which returns Options and does not throw exceptions. A Convert can be added as an implicit parameter to conversion or parsing methods, allowing callers of the method to decide which behaviour they desire.

The README for the repo describes how to use Convert both as a library client and an API creator. Feedback both about the documentation and the library’s design is greatly appreciated. If you find the library useful, let me know!

Please note that this version is a pre-release, and it is not recommended to use it in production environments.