Skip to main content
Generic structs and type aliases exist only at the type level and incur no runtime cost.
Example:

Type arguments

When referencing a generic Container, T must typically be provided:
For generic functions, type arguments can be inferred from the call site:
When calling a method on a generic type, type arguments may be omitted if they can be inferred:

Default type arguments

A type parameter can have a default type: <T = xxx>. Such type parameters may be omitted at use sites.
If all type parameters have defaults, they can be omitted:
Type arguments cannot reference one another; expressions such as T2 = T1 are invalid.

Type aliases

Generic parameters are supported not only by structs, but also by type aliases. The following example defines a generic type alias Response<TResult, TError>:

Methods

Methods for generics are declared exactly as for regular structures. In this form, the compiler treats T, which is unknown symbol, as a type parameter:
Without self parameter, a method will be static.