Return types
- To make sure that the method returns the type that you want
For example:
The return type follows the type grammar.
Marking a method as returning will make it return nil
regardless of what it actually returns:
- Making sure a method returns
nil
without needing to add an extranil
at the end, or at every return point
These methods usually imply a side effect.
Using Void
is the same, but Nil
is more idiomatic: Void
is preferred in C bindings.
NoReturn return type
Some expressions won’t return to the current scope and therefore have no return type. This is expressed as the special return type .
This is for example useful for deconstructing union types:
The compiler recognizes that in case string
is , the right hand side of the expression string || raise
will be evaluated. Since typeof(raise "Empty input")
is NoReturn
the execution would not return to the current scope in that case. That leaves only String
as resulting type of the expression.
Every expression whose code paths all result in NoReturn
will be NoReturn
as well. does not show up in a union type because it would essentially be included in every expression’s type. It is only used when an expression will never return to the current scope.