Null pointer exceptions can be prevented at compile time using a simple data type borrowed from Haskell.
data Maybe a = Nothing | Just a
The type class encoding in my previous post on the subject is a bit rubbish:
The Maybe
type I described could do with some type class instances, so
let’s try out a new encoding.
package gettyped | |
sealed abstract class Maybe[A] { | |
def fold[B](nothing: => B, just: A => B): B = this match { | |
case Nothing() => nothing | |
case Just(a) => just(a) | |
} | |
} | |
private final case class Nothing[A]() extends Maybe[A] {} |
I hereby claim:
To claim this, I am signing this object: