anonymous functions

Anonymous functions

Cyan supports statically-typed anonymous functions, which are called blocks in Smalltalk. An anonymous function is a literal object that can access local variables and fields. It is delimited by { and } and can have parameters that should be put between “(:” and “:)”. The return value is put after “->”. The function is called using the eval method. The return value of the function is the expression following the symbol “^”. The return value type may be omitted in the declaration — it will be deduced by the compiler.

var b = { (: Int x -> Int :) ^x*x };
let f = { (: Byte a, Byte b -> String :) ^
"(" ++ a ++ ", " ++ b ++ ")" };
let z = { ^0 };
let sky = { "sky" println };
assert b eval: 2 == 4;
assert f eval: 2, 5 == "(2, 5)";
assert z == 0;
sky eval; // print "sky"