null. Instead, they have types called Maybe or Optional — basically a box of 0 or 1 items. Effes is going to take that approach, but I might put a twist on it.In a nutshell, the idea behind
Maybe (I'll settle on Haskell's terminiology) is that there's a Nothing that represents the absense of something, a Just Foo that represents one Foo, and a Maybe Foo type which can be either a Nothing or a Just Foo.Like other functional languages, Haskell has syntax (called pattern matching) that's kinda-sorta like an
instanceof check plus a downcast in Java. Putting it all together looks something like this:sayHi :: (Show e) => e -> String sayHi Nothing = "Nothing to say hi to!" sayHi (Just e) = "Hello, " ++ (show e) ++ "!"
(The
(Show e) => syntax just means that e has a show method, which is like Java's toString.) In Effes, a direct translation would be a disjunctive type:data Nothing
data Just[A] = elem : A
type Maybe[A] = Nothing | Just[A]
sayHi (m:Maybe[A Stringable]) -> String:
case m of
Nothing: "Nothing to say hi to!"
Just e: "Hello, {{e}}!"
Because Effes has a more flexible type system, we can actually get away without the
Just part of the Maybe pair. Instead, it looks something like this:data Nothing
type Maybe[A] = Nothing | A
sayHi (m:Maybe[A Stringable]) -> String:
case m of
Nothing: "Nothing to say hi to!"
e: "Hello, {{e}}!"
There's not a really strong driving force for this, except that it seems a bit cleaner. Instead of a
Maybe being "either nothing or a box of one something," it's "either nothing or one thing." Plus it takes advantage of my cool new type system, so that's nice too.The problem is when the
A type is itself a Maybe: Maybe[Maybe[A]]. If we see that it contains a Nothing, does that mean we didn't have anything, or that we had one Nothing? To prevent unreachable code, I'd probably want the type checker to reject this altogether: Maybe[Maybe[String]] would be a type error.That's not terrible, I guess, but the erroring type could be nestled in some data structure. For instance, if a linked list uses
Maybe to signify an end, then LinkedList[Maybe[String]] wouldn't compile — and probably with some unintuitive or frustratingly un-actionable error message.On balance, I'm leaning towards keeping the
Just type. It doesn't add much complexity to the Maybe type, pattern matching keeps the call sites simple, and it eliminates ambiguity.