Generic Functions

Learn how to work with generic functions and use type parameters.

Type parameters

Just as we can pass an argument value to a parameter, we can pass a type as a type argument. For this, a function needs to define one or more type parameters inside <> brackets immediately after the fun keyword.

Tip: By convention, type parameter names are capitalized.

  • When a function defines a type parameter, we have to specify the type arguments when calling this function.

  • The type parameter is a placeholder for a concrete type.

  • The type argument is the actual type that is used when a function is called.

  • To specify type arguments explicitly, we also use <> brackets.

Get hands-on with 1200+ tech skills courses.