4 ms·
How is error handling done if something goes wrong in one of the functions?
by maxxxxx 9y ago
How is error handling done if something goes wrong in one of the functions?
- m1el 9y agoThe answer to that is `Either` monad (similar to `Result` in Rust).
- thomasahle 9y agoYou have to then combine multiple monads though, which isn't quite as easy iirc.
- loverofthings 9y agoWell, monad transformers make things easy but the abstraction impacts runtime performance :(
- js8 9y agoIt depends. In Haskell, functions do not fail in traditional sense - they just return indication of failure. So the return type of function can be, for instance, Either type (as someone already noted) which basically lets you have proper value and error value, one or the other. And Either is also a monad. Typically, you would derive your own monad by somehow combining the two monads (for instance using a monad transformer) that would have a behavior you want. In ordinary languages, the error handling is done ad hoc, in each situation the semantics can subtly differ. You can do that too but then you have to explicitly handle the fact that functions return Either in the function body (this would be somewhat similar to checked exceptions). Or you can define your own monad that prescribes the correct semantics of interaction between the monads.
- gmfawcett 9y ago> In Haskell, functions do not fail in traditional sense - they just return indication of failure. That's too general a statement. Haskell functions certainly can fail in the traditional sense. As a trivial example, "head []" is a well-typed Haskell expression that will raise an exception when evaluated; if the exception is not caught (using a traditional exception handler), then it propagates to an error, and the program exits. This article [1] about the many ways to handle errors in Haskell is a bit old, but as far as I know, all his points are still relevant. The conclusion (for me) is that if modern Haskell tends to use return-values on failure, it's due to convention (and good taste) among developers, and not due to a design constraint of the language. [1] http://www.randomhacks.net/2007/03/10/haskell-8-ways-to-report-errors/ http://www.randomhacks.net/2007/03/10/haskell-8-ways-to-repo...
- pjmlp 9y agoYou end up with something that F# community calls railroad programming, each monad works like a rail switch driving which of the functions actually gets called on it. So if they are in an error state it is like a nop, until someone actually retrieves the error value.
- seanmcdirmid 9y agoThis is what I don’t get about FP, in exchange for code that looks more elegant, you get something that is much more of a PITA to debug. If there is a problem, I often find getting rid of the indirected plumbing and directly exposing control flow to be the best way to find out what is going on when and where.
- pjmlp 9y agoIt is only a PITA to debug on paper, not when using a graphical debugger.
- seanmcdirmid 9y agoHow so? Visual studio still doesn’t reveal the right context when you actually find out the error has occurred.
- pjmlp 9y agoVisual Studio isn't the only option. https://blog.oz-code.com/debugging-linq-available-tool-comparison/ https://blog.oz-code.com/debugging-linq-available-tool-compa... https://www.oz-code.com/ https://www.oz-code.com/
- seanmcdirmid 9y agoNice! Tooling is a large part of my trouble with FP, better tooling would make it easier to debug, especially those complex signal networks that require different debugging paradigms from imperative languages.