4 ms·
The gyrations needed for error handling in functional programming are painful. As Rust libraries become more functional, Rust is getting more of those problems
by Animats 10y ago
The gyrations needed for error handling in functional programming are painful. As Rust libraries become more functional, Rust is getting more of those problems.
The problems created by not having exceptions are tougher than the problems created by having exceptions.
- techdragon 10y agoErlang style "fail noisily and let the supervisor handle it" is quite a solid way to deal with "exception handling bloat" in any paradigm, functional or otherwise... The hard part is finding a way to create effective layers of supervision.
- hvidgaard 10y agoAn exception is a noisy failure, and the supervisor is whatever try/catch that it bubles up to. Still, Erlang style has it's own complexities. It is in essence a network of micro services, and creating that overlay network and manuvering in it isn't simple.
- aangjie 10y agoYep.. with python assert statements + supervisorctl has been a very good setup for scaling a low latency, clean chat filtering app.
- hvidgaard 10y agoF# has exceptions. It's a tool that has it use at times, but they are in essence a goto
- brian_cloutier 10y agoEvery single control flow tool you've ever used is in essence a goto. Every conditional, every loop, every function call. Many modern function calls are in some respects _worse_ than gotos ever were, you don't even know which code you're going to jump to. It's not goto's that were so painful, it was unstructured gotos. Calling exceptions gotos is not a reasonable criticism. They have some very well-defined structure, and additional benefits like separating your happy-path code from your error-handling code, making both simpler.
- mafribe 10y agoIt's important that there are two very different approaches to control flow manipulation. They differ in when the target of a jump is bound. - Static binding of location to jump to. This is what goto, callcc, while, for etc. do. - Dynamic binding of location to jump to. This is what exceptions do. You can show that they are really different, in the sense that (in the absence of powerful language features), they cannot simulate each other, see [1]. Once you realise that exceptions and gotos are quite different thinking about error handling becomes more clearer. [1] J. G. Riecke, H. Thielecke, Typed Exceptions and Continuations Cannot Macro-Express Each Other.
- deleted 10y ago[deleted]
- hvidgaard 10y agoI accidently hit downvote on your post, and I cannot undo it. I hope some kind soul will upvote you to counter. Your post has solid information for the topic at hand.
- brian_cloutier 10y agoThat's a really cool result, thanks for pointing it out!
- hvidgaard 10y agoThe comparison to goto was not meant as criticism, but because both disrupt the control flow in a way that has no structure. Conditionals, loops, and calls do have structure, they are the flow. When a language support contructions such that normal control flow event are either "success + result", or "failure + error", it's much easier to follow, for me, and experience has taught me that it is far less error prone. Exceptions have a use for things that are unexpected, but happened anyway, such as OutOfMemoryException.
- codingmyway 10y agoIn essence exceptions are an implicit variant (discriminated union) extension of the return type of a function. So a function returning T can actually return T or one of the set of Exception types.
- piaste 10y agoNo, I think there's an essential difference here. The return type of a function is always handled by its direct caller and only by its direct caller. Exceptions are fundamentally different because they aren't necessarily handled by the caller, but by the nearest appropriate exception handler, wherever it is, regardless of scope. That is both their strength when they are used appropriately, and their weakness when they are not.
- codingmyway 10y agoYou're right in that the implicit bit extends all the way up the call stack. They aren't the same in practice, just in theory. That implicit parallel 'track' is useful in doing the Foo => Result<Foo, Error> for you in the form of Result<Foo, Exception> but bad in not warning of unmatched values and hiding the fact. Like many a tool it can be abused by those who know just enough to use it but not enough to know why. This is why Java originally forced you to make thrown exceptions explicit. This railway approach is one good way of dealing with error conditions explicitly.
- codingmyway 10y agoExceptions as discriminated unions in C# http://tomasp.net/blog/2015/csharp-pattern-matching/ http://tomasp.net/blog/2015/csharp-pattern-matching/
- cbd1984 10y agoHaskell has exceptions and monads. Either can be used for error handling; monads are, admittedly, unusual, but the error-handing type monads (as opposed to side-effect-handling type monads) are easy to understand in terms of wrapping values (or the potential for a lack of value) such that the type system can be used to both enforce a certain level of care and enable a certain insensitivity to errors. For example, the Maybe monad is the simplest way to handle a function which might not return a usable value: A value wrapped in a Maybe monad has a type of either (Just a) (with a being any type at all) or Nothing. Here's a trivial example, straight from the ghci REPL: > let mdiv x y = if y == 0 then Nothing else (Just (x / y)) > let foo x = (Just (x + 5)) > let bar y = (Just (y / 2)) > mdiv 5 6 >>= foo >== bar Just 2.9166666666666665 > mdiv 5 0 >>= foo >>= bar Nothing See? Complete insensitivity to errors until the last possible moment. Much less noisy than the C equivalent would be, although in this specific example I suppose the Inf semantics of the IEEE floating point standard would be equivalent.
- wldlyinaccurate 10y agoI personally find this "railway" approach (which is essentially a glorified Either) much less painful than dealing with exceptions. Did you really find the example imperative error handling code more elegant than the railway-style function composition?
- cousin_it 10y agoCompared to exceptions, the Either approach to error handling has some big problems: 1) Good luck annotating all potential cases of divide by zero or IO error, and all callers of those. 2) Everyone must pay the runtime cost of wrapping and unwrapping on every operation, even if nothing fails. 3) The type of a function that can fail in ways A and B isn't the same as the type of a function that can fail in ways B and A. 4) Good luck getting a stack trace. I think unchecked exceptions are better for most applications, where errors are rare and have many different types.
- tigershark 10y agoDid you even read the article? For the points 1 and 4 F# has exceptions that are handled seamlessly with this technique using a simple function in the chain, as clearly showed in the article. For point 2 you are just using a different operator from the normal composition one. I don't understand your point 3.
- cousin_it 10y agoI've read the article and don't completely understand your comment. For point 1, if you want to use the railway approach instead of exceptions, you must annotate all functions in the chain. For point 2, I was talking about the runtime cost. For point 3, Result<Result<Foo,A>,B> isn't the same type as Result<Result<Foo,B>,A>. People in the Haskell world acknowledge that it's a big problem ("monads don't compose"), hence all the research on monad transformers and extensible effects etc. For point 4, if your function is the lowest in the call stack and decides to report an error using the railway approach, you don't get a stack trace.
- cel1ne 10y agoInteresting related post: http://blog.paralleluniverse.co/2015/08/07/scoped-continuations/ http://blog.paralleluniverse.co/2015/08/07/scoped-continuati...