3 ms·
The relevant gotcha is when doing lazy IO in Haskell. The general answer is "don't do that" - there are a number of approaches for how. (Lazy IO is fine and q
by dllthomas 12y ago
The relevant gotcha is when doing lazy IO in Haskell. The general answer is "don't do that" - there are a number of approaches for how. (Lazy IO is fine and quite convenient for things of roughly the scale of a shell script. It's not a good fit to bigger things.)
- taeric 12y agoI'm not sure I see how this changes much. My point is that with laziness, your explosion doesn't happen right where you unwrap a Maybe in the code. It happens where you evaluate the unwrapping. Apologies for the ninja edit above, but this scala seems to show it isn't just IO that is at danger here. class Foo(x : =>String) { def happy = println("I'm good.") lazy val ack = x def sad = println(ack) } Again, if I do "new Foo(None.get)", I can call happy as much as I want, but "sad" will blow up. Now, I do not and could not claim that this sort of nonsense would have proliferated with the same abandon as null. I am claiming that this is not that unlike a null pointer exception, though.
- dllthomas 12y agoI think the equivalent to None.get is fromJust Nothing. And yes, it's the equivalent of an unchecked nullable. The Haskell community generally frowns on fromJust (along with other unnecessarily partial functions). The better approach is to either pattern match: case maybe_string of Just string -> newFoo string Nothing -> defaultFoo Or to use one of the relevant higher-order functions: fmap newFoo maybe_string :: Maybe Foo maybe defaultFoo newFoo maybe_string :: Foo
- tel 12y agoYeah, exception resolution is pretty weird in Haskell due to laziness. In some sense, you end up thinking of exceptions as being buried in values in the language instead of exposed by computation—this is your null-value landmine problem exactly http://research.microsoft.com/en-us/um/people/simonpj/papers/imprecise-exn.htm http://research.microsoft.com/en-us/um/people/simonpj/papers... This is also why the Control.Exception module exposes `evaluate`, which embeds a value in an IO computation and thus resolves this problem... If you remember to use it. http://hackage.haskell.org/package/base-4.7.0.0/docs/Control-Exception.html#g:8 http://hackage.haskell.org/package/base-4.7.0.0/docs/Control... Fortunately, in idiomatic Haskell exceptions are considered to be an expert feature (basically I see them in concurrent IO or resource management code only) and partial functions—usually introduced by incomplete pattern matching like `None.get` does—are veboten. It's not perfect, but -Wall will help with that.