Rouge Documentation Help

Generalizing behavior

Previously, you have been promised that functions, besides types, can also be parametrized with placeholders, which this chapter aims to show. Parametrizing a function with a placeholder type works similarly to parametrizing a type, as can be seen in the following example. In the example, the function last_item always returns the last item of a list. The placeholder called T symbols the type of item in the list. The List type also requires a placeholder, which is why we can pass through T as its placeholder. Since the last item in the list is also of type T, last_item returns T as well.

function last_item<T>(list: List<T>) -> T { return list.at(list.size() - 1) }

We can call the function with any list as its parameter, however, we need to pass a type for T in angled brackets. We would like to call last_item on a list of numbers; therefore, T needs to be set to Number.

numbers = List<Number>() numbers.append(1) numbers.append(2) numbers.append(3) print("${last_item<Number>(numbers)}")

Output:

3

Similarly to functions, methods can also make use of placeholder types. In fact, placeholders are defined and used in the exact same way on methods than they do on functions.

Last modified: 07 January 2026