Pony has a single exception value, which isn't parameterised.
Unfortunately frequently I have code which wants to throw different kinds of exception, and I need to know what they are in order to handle them properly --- for example, trivially, when halting the program it's important to give the user the right error message!
So what's the idiomatic way of doing this in Pony?
Currently the idiomatic way would be to not use errors and instead use union types of the normal return type and the error reasons. For example, if you have a function that returns ReturnType and can error with either Error1 or Error2, your function signature can be
fun foo(): (ReturnType | Error1 | Error2)
The caller function must then match on the union to see what happened.
If you want some real-world examples, the files package of the standard library uses this idiom extensively.
Also, for completeness, an RFC on adding typed errors is currently being discussed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With