3 ms·
This was the first thing I thought of. Addition types with pattern matching (which enable Maybes) both provide safe options for these data types. An example Has
by tel 16y ago
This was the first thing I thought of. Addition types with pattern matching (which enable Maybes) both provide safe options for these data types. An example Haskell type that keeps the surface idea of nulls (via Maybe having an object) might be something like:
List a :: Cons a (Maybe (List a))
Tree a :: Branch (Maybe (Tree a)) (Maybe (Tree a))
The type system removes the concept of 'dereferencing nulls' while maintaining that if the pointer isn't null it still must be the (polymorphic) type a.
Of course, the simpler implementation unfolds the addition type inside Maybe and is more like:
List a :: Cons a (List a) | End
Tree a :: Branch (Tree a) (Tree a) | Leaf a