← Back to Kotlin Course | Chapter 11: Generics | Lesson 2 of 6

Generic Functions

A generic function is a recipe that can work on any type of ingredient you hand it, figuring out the type fresh every time it's called.

Declaring a Generic Function

The type parameter goes in angle brackets right before the function name, letting the function's parameters and return type all refer to that same placeholder type.

Example: Declaring a Generic Function

markup
fun <T> identity(value: T): T = value

fun main() {
    println(identity(42))
    println(identity("Kotlin"))
}

Generic Functions on Collections

Generic functions are especially common for working with collections of any element type, such as returning the first two items regardless of what type they are.

Example: Generic Functions on Collections

markup
fun <T> firstTwo(items: List<T>): List<T> = items.take(2)

fun main() {
    println(firstTwo(listOf(1, 2, 3, 4)))
    println(firstTwo(listOf("a", "b", "c")))
}

Multiple Type Parameters in a Function

A generic function can declare more than one type parameter, useful for operations that combine two different kinds of values into a result.

Example: Multiple Type Parameters in a Function

markup
fun <A, B> combine(a: A, b: B): String = "$a and $b"

fun main() {
    println(combine(1, "one"))
    println(combine(true, 3.14))
}

Without a Constraint, T Is Treated as Any?

With no upper bound specified, T behaves like Any?, so the function can only do things valid for absolutely any type, like storing, returning, or comparing for equality.

Example: Without a Constraint, T Is Treated as Any?

markup
fun <T> printTwice(value: T) {
    println(value)
    println(value)
}

fun main() {
    printTwice(100)
    printTwice("hello")
}
Common Mistakes
  1. Placing the type parameter after the function name instead of before it; the correct order is fun <T> functionName(...).
  2. Assuming a generic function can perform type-specific operations (like arithmetic) on T without any constraint restricting what T can be.
  3. Redeclaring a type parameter already present on the enclosing class when it is not actually needed for that particular function.
Chapter Summary
  • A generic function declares its type parameter before the function name: fun <T> functionName(...).
  • The type parameter can appear in the parameter list, return type, or body of the function.
  • Without constraints, T is treated as Any?, so only operations valid for any type (like storing or returning it) are allowed.
  • Generic functions let one implementation work correctly and safely across many different types.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.