Sometimes, you have a type that should behave like pointer. For example you may want to compare is with nil (value == nil). But this (and similar variants with generics) don't work:
proc `==`(m: MyType, t: type(nil)): bool =
return m.isNil
It would be great to have normal type type(nil) or NilType that has only one instance (nil).
This may be relatively simple to implement actually. There is already an internal type called tyNil:
https://github.com/nim-lang/Nim/blob/devel/compiler/ast.nim#L326
I don't see a reason not to support it. Here a comparison of the scala repl and the Nim repl with the same code.
Nim:
>>> let a = nil
stdin(1, 5) Error: invalid type: 'nil'
Scala:
val a = null
a: Null = null
C++ also allows it (even though that in not necessarily a good point, until recently c++ did not even have a nil literal)
auto a = nullptr;
The Scala developers thought it is a
Playing devil advocate here. Isn't that the very use case of the Option type?
Among functional and imperative languages:
Haskell doesn't have null/none/nil but use Maybe which is equivalent to Option.
Rust uses Option as well.
Note on ergonomics
I like Haskell clean use, thanks to it's pattern matching.
Rust however is a big pain as you have to add unwrap everywhere you need the wrapped value.
@mratsim no, the nil type is not the option type. The nil type is the type of the nil literal, the option type tries to get rid of nil pointers at all.
The nil type can be written as type(nil).
Most helpful comment
The nil type can be written as
type(nil).