Does Mojo have a null safety feature like Dart?
Sound null safety
Information about Dart's null safety feature
3 Replies
Aside from
UnsafePointer
, which needs to be nullable for C interop, Mojo does not have null. You use Optional
instead.Mojo does not have nullDoesn't
None
act as Mojo's null
?None is its own type, which is mostly used for "there is nothing here". Types don't have the implicit "There may or may not be something here" union that many languages have which leads to null safety issues.
None
can be cast into an Optional
without a value in it, but this means that unless a function returns Optional[T]
, it will always return a valid instance of T. A function can also return None
, meaning no return type, which is useful for generics.