generics

Types that take other types as parameters, so one definition works for many kinds of value.

In more detail

A generic function or type has placeholders, such as T, that are filled in with real types where it is used. Generics let a library keep your specific types all the way through its code.

Examples

public static T[] Repeat<T>(T value, int count)
ProcedureDef<TIn, TOut> fills TIn and TOut with each procedure’s real input and output types.
Java: public class GenericBox<T> { private T value; }
Java generics are checked at compile time and mostly erased at runtime.
public class GenericBox<T> { private T value; }
List<String> names = new ArrayList<>();
function firstItem<T>(items: T[]): T | undefined { return items[0]; }
new Map<string, number>() passes explicit type arguments because an empty collection gives nothing to infer from

Related terms

Taught in

References

Certification courses