DEV Community

Discussion on: Is an exception less (pure) than a Result?

Collapse
 
iquardt profile image
Iven Marquardt • Edited

An exception hampers the function call stack by unwinding it. That's the problem. It is an computational effect that happens outside the realm of a pure function.

So what can we do about it? Your observation is right: A partial function that exhibits the same exception for the same input is deterministic. So we have to turn the effect into a value in order to render this function pure. In the functional paradigm there are Maybe/Option and Either types to encode exceptions. Instead of unwinding the call stack these types short circuit code, which can cause the stack to unwind as well, but this time with the means of pure functions.

Collapse
 
misobelica profile image
Mišo

Thanks for the answer. It forced me to search for stack unwinding and I found some interesting sources and added them at the end of the article. I still don't understand why this is a problem, but I hope it will help me to learn more about it.

From the various discussions, I noticed the problem is actually a reaction to the exception not throwing it. It's that the control flow changes and it's like a GOTO jump. But still, even function call is implemented as a jump in low-level assembly code and that is not a problem. I guess I need to dig deeper.

Have a nice day :)