- 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
Evaluating an anonymous function
Anonymous functions or just functions can pass messages through the eval keyword. In this case, the message name is ‘eval’ and the receiver is ‘f’.
var Int n = 0; var f = { n = 1 }; f eval;
This is similar to f.eval()
in Java/C#/C++. f eval passed a message to f evaluating { n = 1 } and the value of n has been changed. We can assert that with assert.
assert n == 1;
The same can be done with functions that have parameters, as the following
var Functiong = { (: Int k -> String :) ^ "k = " ++ k; }; assert g eval: 5 == "k = 5";