M
Modular4d ago
name

Does Mojo have a null safety feature like Dart?

Sound null safety
Information about Dart's null safety feature
3 Replies
Darkmatter
Darkmatter4d ago
Aside from UnsafePointer, which needs to be nullable for C interop, Mojo does not have null. You use Optional instead.
Heyitsmeguys
Heyitsmeguys4d ago
Mojo does not have null
Doesn't None act as Mojo's null?
Darkmatter
Darkmatter4d ago
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.

Did you find this page helpful?