2 ms·
This just defines a new type (called Maybe). The Maybe type can either contain a value or Nothing. This is useful in Haskell because regular values cannot be nu
by chongli 14y ago
This just defines a new type (called Maybe). The Maybe type can either contain a value or Nothing. This is useful in Haskell because regular values cannot be null. When a function has the potential to fail, it can use one of these Maybe types as its return type to let the caller know of (and handle) the possibility of failure.
As for the syntax itself? It's called a tagged union. Just and Nothing are defined to be constructors for building values of type Maybe a. Just takes one argument (of any type) and Nothing takes no arguments.
Just "hello"
This has type Maybe String.
Nothing
This might also have type Maybe String (or it might not). A function which accepts a Maybe String as one of its arguments will accept Nothing as well.