I've been following the official documentation of Crystal but I couldn't find any details on this. The regular syntax when declaring a union type is String | Int32
. However, I've noticed a difference regarding the Nil
type.
The regular way of declaring the union still works:
def foo(id : String | Nil)
end
# Overloads are:
# - foo(id : String | Nil)
However I've also seen a shortened syntax which I couldn't find any documentation for:
def foo(id : String?)
end
# Overloads are:
# - foo(id : String | ::Nil)
The result is almost exactly the same except Nil
if prefixed with 2 colons. My guess that this is something related to the global scope of Nil
as I've seen a similar syntax in other languages.
- Is
String | Nil
andString?
the same thing, and when should you use one vs the other? - What's the meaning of 2 colons in the type signature (e.g.
::Nil
)?