List and array expansion operator *.

Gosu provides an operator for array expansion and list expansion that expands and flattens complex object graphs. This expansion can be useful and powerful, enabling you to extract one specific property from all objects several levels down in an object hierarchy. The expansion operator is an asterisk followed by a period. For example:

names*.length

Using the expansion operator on a list retrieves a property from every item in the list and returns all instances of that property in a new list. The expansion operator works similarly with arrays.

Let us consider the previous example names*.length. Assume that names contains a list of String objects, and each one represents a name. All String objects contain a length field. The result of the previous expression is a list containing the same number of items as the original list. However, each item is the length (the String.length property) of the corresponding name String object.

Gosu uses Gosu generics, an advanced type feature, to infer the type of the list as the appropriate parameterized type. Similarly, Gosu infers the type of the result array if you call the operator on an array.

See also