Void
is a synonym for Nil
. This is an aspect of the Crystal language which I find confusing, asymmetrical, or less than optimal. It is perfectly valid to take the value of a function returning Void and call methods upon it, as in:
def foo : Void
end
p foo.nil?
My feeling is that a first-class Void
would not be a value and that it would be an error to assign it or call operators or methods upon it, and that this would contribute to code-correctness. This would also be useful in other contexts, for example declaring the top-level self
to be Void
would handle what seems to be single-purpose code today in a more generalized way.
I think Crystal has inherited some confusion from C, in that C void
is a declaration that a function returns no value, and the C void *
is pointer to untyped memory, and the reference to it can be cast or initialized into something with a value. This is worse in Crystal because references are implicit. IMO Void
should be first-class and Untyped
should be its own thing.
More confusing: Crystal uses NoReturn
to refer to an invalid value and to mark functions as ending the flow-of-control.