3 ms·
"Things like Either[A,B] make me cringe because they're just like what we used to do back in the 1980's in C." In C(++/#), yes. In Haskell, there's two absolu
by jerf 15y ago
"Things like Either[A,B] make me cringe because they're just like what we used to do back in the 1980's in C."
In C(++/#), yes.
In Haskell, there's two absolutely critical differences, which are worth pointing out, because while it's fun to raid Haskell for ideas it's important to understand the context from which they come or you'll end up thinking good ideas are just crazy. (I want to see other languages raid Haskell, but it ends up being very easy to copy the form and entirely, utterly miss the substance. I see the same problem with Erlang copiers.)
First, the compiler forces you to unpack the error. Now, you can still write code that discards the error or fails to handle it, but you can't simply forget.
Second, you can compose these together:
bigOperation = do
a <- eitherOperation
b <- eitherOperation
c <- someOtherEitherOperation
return $ a + b + c
With the do notation in the Either monad, you'll get the first error in the sequence, or the final result; you do not need to handle each error individually. This is a choice you make on purpose; if you want to, you can, if you want to handle errors in some other manner, you can, etc, this is not "the" error handling technique in the language, it's just one that ships in the base library. Personally I don't much like it because people tend to use Either with a string in the error position, and I really hate it when strings are the base error type.
(Also, there is a bit of a problem composing together different bits of code that handle errors differently, which is a disadvantage of giving the programmer so much flexibility. The exceptions mechanisms may not be perfect but they are consistent.)
And, indeed, just copying the Either type into C(++/#) is copying the form and missing the substance. I don't know what the solution for C(++/#) is, none of those languages have rich enough abstraction to copy Haskell here.
- jauco 15y agoIIRC in c# you can abuse selectmany and it's inline linq syntax to achieve more or less the same effect. (google "monad selectmany" for examples)
- earl 15y agogroovy helps a bit with this when you use the jvm -- lots of their operators are null aware [1], ie max([3, null, 4]) == 4 and they have a null-safe dereference operator [2], so returning null is actually reasonable as an error def user = User.find( "admin" ) //this might be null if 'admin' does not exist def streetName = user?.address?.street Collapsable to: def streetName = User.find("admin").?address.?street It's not perfect, but it's a lot better than plain java. Compare to: String street = null; User user = User.find("admin"); if (user != null) { Address addr = user.address; if (addr != null) street = addr.street; } // or even String street = null; if (User.find("admin") != null && User.find("admin").address != null) street = User.find("admin").address; I think one of my biggest complaints -- and the reason I think Gosling is a moron -- is how programmer unfriendly java is. Some syntactical sugar as above, so you don't have to nest null checks, goes a long way towards making languages more friendly to devs. Even simple things like in ruby being able to say 1_000_300_233 == 1000300233 makes it a lot nicer to read big numbers. Sigh. If Gosling actually wrote code instead of doing whatever he wastes his time doing, you'd think that's the sort of thing you could just add to the language. [1] http://groovy.codehaus.org/Null+Object+Pattern http://groovy.codehaus.org/Null+Object+Pattern [2] http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29 http://groovy.codehaus.org/Operators#Operators-SafeNavigatio...