r/AskProgramming • u/AnOwlishSham • Dec 13 '24
Using Result with a default exception instead of using Optional?
I wanted to extend the API of Java's Optional and also to have a Result type wrapping a value or an exception.
After working with some other libraries I ended up writing my own to provide this. After working on Result I turned my attention to an enhanced Optional. While thinking through this, and realising that all the methods I wanted to add to Optional were already in Result, the thought occurred to me whether Optional could be viewed as just a degenerate Result – that is, a Result with a default exception.
Based on this I decided not to provide an enhanced Optional and simply have Result. So now in my code when I might otherwise have returned an empty Optional, I now return a Result.failure() and gain access to the enhanced API this offers.
In using Result over Optional I realise I am substituting the concept of success/failure for presence/non-presence. But for scenarios such as looking up a configuration setting that is missing, not finding a record in a database, or getting the maximum value from an empty collection, it seems to me to be reasonable to think of these as 'failures'.
Is there a flaw in my analysis?
I also opted not to provide Either, which is often found in functional libraries. While there are other uses for Either, most of the examples I encountered were about handling success/failure, where the 'other' value is of some type representing an error. But as I was happy with using the Exception type for that purpose, as provided by Result, there didn't seem to me to be a need to provide Either.
1
u/WaferIndependent7601 Dec 13 '24
Why don’t you just throw the exception and catch it where you want to handle it?
1
u/pthierry Dec 15 '24
What is there in Result that's missing from Optional?
2
u/SnooRecipes5458 Dec 13 '24
Coming from the r/Kotlin crosspost.
The semantics of Optional are that something can have a value or not. Kotlin's nullable types avoid the need for an optional type, although there are limitations.
If you want to denote that something can be A or B (a failure in this case), then use Result (like in Kotlin stdlib) or an Either.
1
u/MadocComadrin Dec 13 '24
Putting aside specific languages and their implementations, you've got the right idea. A Result is often just an Either (general tagged union of two types) with different names to signify a specific use case, and an Optional(/Option/Maybe) over your desired type is similar (technically isomorphic) to an Either where the left type is your desired type and the right type is a type with a single value.
If it makes sense for your use case and Java's Optional doesn't get any special treatment under the hood, go for it.