evaluating anonymous functions

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 Function g = { (: Int k -> String :) ^ "k = " ++ k; }; assert g eval: 5 == "k = 5";