Context objects

A context object is an object of a prototype that can take, in its constructor, a variable:

object ConcatStr<T>(String &sum, String between)

Here, sum is a reference to a variable passed as argument in the creation of an object of ConcatStr. If the object methods change sum, the original variable passed as argument is changed too. The argument can only be a variable or field. Hence, sum is much like a language C pointer. This prototype defines a constructor in its declaration that takes two strings.
Let us see an example.

Generic prototype ConcatStr inherits from Function<T, Nil> and, therefore, it can be used where an anonymous function that takes a T parameter and does not return anything is expected.

package cyan.lang object ConcatStr<T>(String &sum, String between) extends Function<T, Nil> override func eval: T elem { sum = sum ++ elem ++ between } end

This prototype can be used as in

var intList = [ 2, 3, 5, 7 ]; var primeList = ""; /* for each list element, call method eval of ConcatStr<Int> for concatenating the already built string, primeList, with " # " */ intList foreach: ConcatStr(primeList, " # "); assert primeList == "2 # 3 # 5 # 7 # ";