r/functionalprogramming • u/ACrossingTroll • Dec 26 '23
Question Deeply nested exceptions
Hi, I'm currently learning FP and many concepts I see I have actually already implemented myself in OOP. One thing though is still a bit fuzzy to me, and that's error handling.
Let's say I want to parse some (string) and either it's valid and returns the parsed value (int) or it returns an error (string). As I understand it, with FP you would use EITHER, with the left (error) and right (happy) path. Afterwards you can nest the error path vs happy path with FLATMAP, effectively passing through the error, or continuing the program flow. So far so good, I hope.
Now my concern is: what if your error check happened 30 levels down the stack? Wouldn't that mean you constantly have to FLATMAP until you finally deal with the error on level 5 etc, like printing it? Also doesn't that mean you pretty much would end up flatmapping your functions all the time because error handling is everywhere? Like writing a "flatmappedfunction" you'd use all over the place?
This is where OOP seems to be much easier. I know it is obfuscating the program flow a bit. But you just would need to throw the exception once and deal at the appropriate place up in the stack. Instead of 30x FLATMAP?
Please correct me if I'm wrong.
View all comments
Show parent comment
9
u/phlummox Dec 26 '23
You're not returning two types, though - you're returning
Either String a, whereais your result type. That's just one type. You can think of it as your result typea, but enriched in some way. Languages with exceptions are effectively doing exactly the same thing, but they're hiding the fact that any function which claims to return anacan actually when invoked produce anaor an exception.You might also decide to abstract the
Either String atype a little asParseResult a, if you think you might later want more information than just an error message.Passing this result up through other layers should be very straightforward - you just express it as function composition. If your layers need to alter the value on its way, your chosen language should provide many ways to do so (operating on the left or the right of the Either, as needed).
In a language with typeclasses and "do" notation, you may be able to make your code more succinct.
And when you need to display the result in the UI, you just pattern match on it.
Plenty of non-FP languages opt to pass errors around explicitly, rather than use exceptions, too - Go and Rust are examples. It really doesn't pose much difficulty - try them, and see.
I'd suggest doing the same for the example you've given here - there's no good way of learning except by getting your hands dirty. Make a mini parser, for, say, YYYY-MM-DD dates, make a small UI to display the result, and experiment. If you're doing things right, it shouldn't be any more code than an OO implementation.