- Installing
- Hello World
- Spaces
- Identation
- Basics
- Comments
- Output
- ++ method
- Assignments and types
- Operator Precedence
- String interpolation
- Input
- Simple arithmetic
- Multiline strings
- Assert
- Arrays and literal arrays
- Literal intervals
- If, for, while and repeat-until
- Message sends
- Conversion between basic type values
- Anonymous functions
- Evaluating an anonymous function
- Functions with method foreach
- Prototypes
- Packages
- Gradual typing
- Generic prototypes
- Important metaobjects
- The CyanInterpreter prototype
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"