Earlier quoted context omitted.
The real trick is that, conceptually, these are not really "unwrapping" functions--instead, they're "propagating" functions. All they do is take Maybe values from deep inside your computation and string them through to the outside. In practice, this means that you write a decent part of your program using these techniques, which creates a block of code that produces a Maybe value after taking a bunch of Maybe inputs.…
>That pattern just turns out to be very useful. Like NaN, it can be hard to track down where things went wrong.
Why Maybe Is Better Than Null
61–70 of 133 posts
Re: Why Maybe Is Better Than Null
#62Earlier quoted context omitted.
Ah yes, wonderfully clear syntax: do a
That was a poor choice. The local “a” shadows the parameter “a”. You can of course write: addM :: Maybe Int -> Maybe Int -> Maybe Int addM ma mb = do a As an aside, the inferred type would be much more general, because (+) works on any number and do/return work on any monad: addM :: (Monad m, Num a) => m a -> m a -> m a Which means you could use it like this: addM (Just 5) (Just 10) == Just 15 But also like this: add…
Re: Why Maybe Is Better Than Null
#63Earlier quoted context omitted.
The real trick is that, conceptually, these are not really "unwrapping" functions--instead, they're "propagating" functions. All they do is take Maybe values from deep inside your computation and string them through to the outside. In practice, this means that you write a decent part of your program using these techniques, which creates a block of code that produces a Maybe value after taking a bunch of Maybe inputs.…
>That pattern just turns out to be very useful. Like NaN, it can be hard to track down where things went wrong.
Re: Why Maybe Is Better Than Null
#64Re: Why Maybe Is Better Than Null
#65In comparison to Haskell, null in Java is similar to adding an implicit Maybe a instance for every type a. In doing so, Java makes it just that much easier to create bottom values when they are not desired (e.g., dereferencing a null pointer).
E foo; ... foo=foo.bar().bat()
without having to do a null check after each function.
I think that Java's approach has the worst of both worlds, because there is no way to make foo.bar().bat() safe when a function could return null.
In C, for example, calling a method on a null object does not cause an error. Rather it passes in null as the 'this' value, allowing you to do you null checks within the method.
Re: Why Maybe Is Better Than Null
#66[deleted]
Re: Why Maybe Is Better Than Null
#67Earlier quoted context omitted.
True. I think it's also a covert argument for static type checking, though ;-).
Give me a statically typed Python and a type system that can handle external hardware poking around in its memory and I'm in ;) I'm sort of a fan of Haskell already, to tell you the truth.
Not sure that's so far from Haskell, really... :-P
> and a type system that can handle external hardware poking around in its memory
Can it be in isolated places? That's doable...
Re: Why Maybe Is Better Than Null
#68[deleted]
If the database structure is encoded in your type system, and all your code that accesses the database (including the initial population of the database) is checked against that encoding, the compiler can absolutely statically check the correctness of code that relies on the structure and contents of the database. Several of the mainstream Haskell database packages do this.
Still, IMO this may be true in theory. In practice, most of the errors we encounter come from incomplete understanding of the input we're dealing with. These are many little rules that are very hard to formalize.
Re: Why Maybe Is Better Than Null
#69[deleted]
Still, on balance I prefer dynamic languages because I don't like to spend time defining and using type relations but I am no longer sure I really know which way is "better" for many types of tasks.
Re: Why Maybe Is Better Than Null
#70If you work on types that might be null, you're bloody careful or you're in the wrong profession. We're not calling incompetent programmers a gazillion dollar mistake, even though they probably have caused as much.