ClassyPimp
ClassyPimp

Reputation: 725

Crystal lang: understanding method return type of Void

It's not documented on the docs. But through experiments I've found out that typing the return type of method as Void, cancels the "last statement return" (that is good) making it returning Nil.

def foo; "baz"; end #=> "baz" : String

def foo : Void; "baz"; end #=> nil

def foo : Nil; "baz"; end #=> nil

The question is:

is above assumption correct?

Is there any difference between Void and Nil method return type?

Upvotes: 1

Views: 268

Answers (1)

asterite
asterite

Reputation: 2926

You are correct. Some last changes in the language didn't get reflected yet in the docs. I guess it's time for me to write some more docs :-)

See this where this was merged: https://github.com/crystal-lang/crystal/pull/2701

So there is no difference between Void and Nil there, and the recommended thing to do is to use Nil in Crystal land (use Void in C bindings)

Upvotes: 2

Related Questions