Crystal has a type called Nil
, which can only have one value: nil
.
It is used to represent the absence of a value, and is similar to null
or None
in other languages.
Nil values can be returned from various methods for example String#[]?
, which returns nil
if the index is out of bounds.
"foo"[10]? # => nil
Crystal has what is known as NULL REFERENCE CHECKS, which means that all types are non-nilable by default.
This means you can not assign nil
to a variable unless you explicitly declare it as a Nil
type.
In turn this means that the compiler will automatically check for null references.
Certain approaches can be taken to handle nil
values.
nil
is a falsey value as well are false
.
This means that statements and other places where a falsey or truthy value is expected it will be treated the same as false
.
if nil
puts "nil is truthy"
else
puts "nil is falsey"
end
# => nil is falsey
This means that the truthy branch won't be taken if a nilable variable has a value of nil
.
foo = "boo"[2]? # It is possible that foo is nil
if foo
p foo.class # => Char
end
Crystal has a method that can be used to check if a value is nil
.
It is called nil?
and it is available on all objects.
foo = "boo"[2]?
if foo.nil?
puts "foo is nil"
else
puts "foo is not nil"
end
This is a bit different when just using the value in an if statement, since in the last examples would both false and nil be treated as false.
Here only nil
is treated as falsey, since if it was false it wouldn't have been nil thereby it would have been truthy.
The easiest way to deal with nil
values is to ensure that they never become nil
in the first place.
The or operator (||
) is often used when dealing with Bools
, but if understood correctly, it can also be used to deal with nil
values.
The or operator checks if the first value is truthy, if not the second value is used.
This can be used to make if the value is nil
it will be falsey and thereby the second value will be used.
"foo"[10]? || "bar" # => "bar"
This approach should be seen as a last resort, and should only be used if you are sure that the value is not nil. If possible use the other approaches instead.
not_nil!
is a method that allows you to tell the compiler that a value is not nil and will thereby make so the type it holds can't be nil.
It does that by raising an exception if the value is nil.
foo = "foo"[4]?
foo.not_nil!
# => Error: Nil assertion failed (NilAssertionError)