How do you declare a generic function in Kotlin, and where does the type parameter go relative to the function name?
answer
- fun <T> name(...) — angle brackets BEFORE the name
- Class is opposite: class Box<T> (after the name)
- T usable in params, return type, and body
- Multiple: fun <K, V> ...
- Convention: T, R, K, V, E
basics
~10 sYou write the type name in angle brackets right before the function name, like fun <T> first(list: List<T>): T. T is a placeholder for any type the caller uses.
solid answer
~40 sA generic function declares one or more type parameters in angle brackets placed before the function name: fun <T> firstOrNull(list: List<T>): T?. Unlike generic classes, where the parameter sits after the class name (class Box<T>), a function's parameter goes before its name because the name is also where the receiver/parameters begin. The type parameter T can then be used in the parameter types, return type, and body. You can declare several, e.g. fun <K, V> pair(k: K, v: V): Pair<K, V>. Type parameters are conventionally single uppercase letters (T, R, K, V). They let one function work over many types with full static type safety, avoiding casts. At the call site the compiler usually infers T from the arguments, so you rarely spell it out.
code
kotlin · 6 linesfun <T> firstOrNull(items: List<T>): T? = if (items.isEmpty()) null else items[0]
fun <K, V> singleEntryMap(k: K, v: V): Map<K, V> = mapOf(k to v)
// extension form — <T> still before the name
fun <T> T.echo(): T { println(this); return this }go deeper
Knows the exact syntax fun <T> name(...) and that <T> precedes the name; can write a simple identity/first function.
Explains the position difference vs classes, uses multiple parameters, and applies generics to extension functions.
Frames generics as static type safety without casts, knows erasure caveats, and uses idiomatic names plus generic extensions fluently.
Reasons about API design: when a generic function vs overloads is appropriate, naming conventions for library readability, and erasure implications for callers.
## What a generic function is A **generic function** is a function parameterized by one or more **type parameters** — placeholders for types that are filled in per call. This lets a single function operate on many types while keeping full compile-time type safety (no `Object`/`Any` casts). ## Syntax: where the `<T>` goes For a function, the type-parameter list comes **immediately before the function name**: ```kotlin fun <T> identity(value: T): T = value fun <K, V> mapOfOne(key: K, value: V): Map<K, V> = mapOf(key to value) ``` Contrast this with a **generic class**, where the parameter list comes **after** the class name: `class Box<T>(val item: T)`. The difference is purely positional: a class name stands alone, so `<T>` follows it; a function name is followed by its parameter list `(...)`, so `<T>` must precede the name to stay unambiguous. ## Using the type parameter Once declared, `T` is in scope for: - **parameter types** — `value: T` - the **return type** — `: T` - the **function body** — you can declare locals of type `T`, call it, etc. You may declare multiple parameters separated by commas: `fun <A, B, C> ...`. Convention uses short uppercase names: `T` (type), `R` (result), `K`/`V` (key/value), `E` (element). ## Extension generic functions The receiver can also be generic: ```kotlin fun <T> T.alsoLog(): T { println(this); return this } ``` Here `<T>` is still before the name `alsoLog`, and `T` is used as the receiver type. ## Why it matters Without generics you'd either duplicate the function per type or accept/return `Any` and cast — losing type safety. Generics give one reusable, type-checked definition. Note: Kotlin generics are **erased** at runtime (like Java) unless the function is `inline` with a `reified` parameter — but that is a separate concern from simply declaring `fun <T>`.
- Why does a generic function put <T> before the name while a generic class puts it after?A class name stands alone so <T> can follow it; a function name is immediately followed by its (...) parameter list, so the type parameters must precede the name to keep the grammar unambiguous.
- Can a type parameter be used in the function's return type?Yes. Once declared in <...>, T is in scope for parameter types, the return type, and the body — e.g. fun <T> id(x: T): T = x.
The <T> before the name is like a blank you fill in on a form before reading the rest of the sentence.
saying these in an interview costs you the question
- Writing fun identity<T>(...) — putting <T> after the name (that's class syntax)
- Thinking generics require runtime type tokens for a plain fun <T>
- Claiming you must always pass the type explicitly at the call site
- Confusing the type parameter T with a regular value parameter
- Saying T must be a specific type rather than a placeholder