Block return types

A block definition does not need to explicitly declare a return type, which is the type of the return value of the block. The return value is statically typed even though the type is not explicitly visible in the code. Gosu infers the return type from either the expression, if you defined the block with an expression, or from the return statements in a statement list. This functionality makes the block appear shorter and more elegant.

For example, consider the following block:

var blockWithStatementBody = \ -> { return "hello blocks" } 

Because the statement return "hello blocks" returns a String object, the Gosu compiler infers that the return type of the block is String.

The following block returns an array containing the square and the cube of the parameter. In this case the syntax {return { expression, expression }} is required because an array of values is returned:

var b1 : block(Integer):Integer[]
b1 = (\ i -> {return {(i * i), (i * i * i)}})
print(b1(2))

This code prints:

[4, 8]