Closures

All Leaf functions are first-class entities and closures by default. This can be seen by looking closely at the syntax of declaring functions.

//an anonymous closure is created and assigned to variable a
var a = -> { 
    code 
}

//so this works
var b = a

//as well as this
fun( a )

Part of the fun of closures is their ability to refer to variables from their enclosing scope.

var create_adder = (x : integer)-> {
    return (y :integer)-> {
        return x + y
    }
}

var add5 = create_adder(5)
//prints 7
trace( add5(2) )

When the function omits a return type it will be inferred. Here the type is (y : integer)->(:integer).