Generics
Generics is a language feature that you use to define a class or function that applies to many types by abstracting its behavior across multiple types of objects. Gosu generics work in a very similar manner to generics in Java.
You use generics to write statically typed code that can be abstracted to work with multiple types. By designing an API to apply to different types of objects, you write the code only once, and the code supports those different types. In this way, you can generalize classes or methods to work with multiple types and retain compile-time type safety.
Generics make the relationships explicit between the types of parameters and return values of a function. Generics
are especially useful if any of the parameters or return value are collections. For example, you can require two
arguments to a function to be homogeneous collections of the same type of object, and that the function returns
the same type of collection. Designing functions to be abstract in that way enables your code and the Gosu
language to infer other relationships. For example, a function that returns the first item in a collection of
String objects is always typed as a String. You do not need to write coercion
code with the syntax as TYPE with functions that use generics. Because
generics increase how often Gosu can use type inference, your collection-related code can be readable, concise,
and type-safe.
See also
