Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

So basically an Either is for returning a different (ostensibly error) type in a language that normally cares about the declared return type in a method signature?

How would that work for passing the result to another method for analysis? Relax the rules there too? Wouldn't that require doing away with compiler/interpretor type enforcement altogether? (not that I have a problem with that)



The type checker will confirm that other function accepts a (Either Bad Good) as its argument. If you want to use a function that only takes a Good and returns a SomethingElse, call it in a "do" block and the call will be skipped if you got a Bad (because in the Either monad, the Left value is used for short-circuit failure) or use liftM to convert it to a function that takes a (Either Bad Good) and returns a (Either Bad SomethingElse).

A lot of other languages had to bake in exception type checking as a weird special case, because even though the type of a function (because that's really about when you can and cannot use it) should depend on which exceptions it handles, they made that way too much hassle to actually enforce.

However, Haskell is still evolving, and people have also added a couple versions of the typical bypass-the-type-system exception trainwreck, for reasons that escape me.

http://www.randomhacks.net/articles/2007/03/10/haskell-8-way...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: