I won’t comment on the idiomaticity(?) or correctness of some of these examples (although I want to!). But I do want to note the pitfall of using a union instead of something like result.Result: in generic code, if the “success” value is an instance of an Exception, you have no way of telling if it succeeded or failed.
I’ve tried using libraries to bring more functional idioms into less-functional languages. purify-ts, and another for python which I forget. Unfortunately, the syntax and/or semantics of the language usually ends up fighting you every step of the way.
There is the Returns library in Python which implements a lot of nice functional pattern, including Result. That look nice, but I feel at the end of the day you need to wrap everything because most of the ecosystem uses exception. There’s also the fact that any single line of Python can raise an exception due for external reasons.
And yeah, enum leave out so much of the reason why you would want to use container type. Result type without the map and all other utility methods is going to end up with a lot of annoying boilerplate code.
I won’t comment on the idiomaticity(?) or correctness of some of these examples (although I want to!). But I do want to note the pitfall of using a union instead of something like
result.Result
: in generic code, if the “success” value is an instance of anException
, you have no way of telling if it succeeded or failed.I’ve tried using libraries to bring more functional idioms into less-functional languages. purify-ts, and another for python which I forget. Unfortunately, the syntax and/or semantics of the language usually ends up fighting you every step of the way.
There is the Returns library in Python which implements a lot of nice functional pattern, including
Result
. That look nice, but I feel at the end of the day you need to wrap everything because most of the ecosystem uses exception. There’s also the fact that any single line of Python can raise an exception due for external reasons.And yeah, enum leave out so much of the reason why you would want to use container type.
Result
type without themap
and all other utility methods is going to end up with a lot of annoying boilerplate code.