Skip to main content
freelanceshack.com

Back to all posts

How to Create A Generic Function In Kotlin?

Published on
7 min read
How to Create A Generic Function In Kotlin? image

Best Kotlin Programming Books to Buy in October 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
3 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
4 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
7 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
8 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.53
Kotlin: An Illustrated Guide
9 Functional Programming in Kotlin

Functional Programming in Kotlin

BUY & SAVE
$46.16 $49.99
Save 8%
Functional Programming in Kotlin
+
ONE MORE?

To create a generic function in Kotlin, you can follow these steps:

  1. Start by defining the function using the fun keyword, followed by the function name and parentheses for parameter declaration.
  2. Inside the parentheses, specify the generic type(s) in angle brackets (<>) after the function name. You can use any placeholder name you prefer, commonly T for a single generic type or T1, T2, etc. for multiple generic types.
  3. Within the function body, you can use the generic type(s) just like any other type. The compiler will infer the specific type(s) when the function is called based on the argument(s) provided.
  4. Use the generic type(s) in function parameters and return type, allowing for flexibility in working with various types.
  5. You can constraint the generic type(s) by upper bounds, using the : Type notation after the generic type(s) declaration. For example, fun > sort(list: List): List ensures that T must be a Comparable type to be used in the function.

Here's an example of a generic function to swap two values:

fun swap(a: T, b: T): Pair<T, T> { return Pair(b, a) }

fun main() { val result = swap("Hello", "World") println("Swapped: ${result.first}, ${result.second}") }

In this example, the swap function takes two arguments of type T and returns a Pair of the same type T. The function can be called with any type, allowing for generic behavior.

Note: Generics provide flexibility and code reusability by allowing functions to work with multiple types. They ensure type safety at compile-time, reducing the chance of runtime errors.

How to handle nullability when using generics in Kotlin?

In Kotlin, you can handle nullability when using generics in a few different ways:

  1. Specify the nullable type explicitly: By default, type parameters in Kotlin are non-null. If you want a generic type to allow null values, you can use the nullable type syntax by adding a question mark (?) after the type parameter declaration. For example: class MyGenericClass
  2. Use the where clause: If you want to ensure that a type parameter is non-null, you can add the where clause to the type parameter declaration. This allows you to specify additional constraints on the type parameter. For example: class MyGenericClass where T : Any
  3. Use platform types with caution: In Kotlin, Java classes with unknown nullability are represented as platform types. When using such types in generic parameters, it’s important to handle nullability cautiously. You can use the safe call operator (?.) or the not-null assertion operator (!!) to access members of platform types.
  4. Use smart casts and null-checks: Kotlin’s smart cast feature allows you to automatically cast a value to a non-null type if a null check has been performed. By using null-checks and smart casts, you can safely handle nullability in generic code.

Here's an example demonstrating the usage of generics with nullable types:

class MyGenericClass<T?>(private val value: T?) { fun printValue() { if (value != null) { println(value.toString()) } else { println("Value is null") } } }

fun main() { val genericClass1 = MyGenericClass<Int?>(null) genericClass1.printValue() // Output: Value is null

val genericClass2 = MyGenericClass<String?>("Hello")
genericClass2.printValue() // Output: Hello

}

In this example, MyGenericClass takes a nullable type parameter T? and the printValue() function safely handles nullability by performing a null check before accessing the value.

What is the difference between reified and non-reified type parameters in Kotlin?

In Kotlin, type parameters can be marked as reified or non-reified, and this distinction primarily affects how these parameters can be used within the function.

Non-reified type parameters are the default in Kotlin. These parameters are erased at runtime and are not available for inspection using reflection. You cannot use them as a type argument directly or access their members. Non-reified type parameters are commonly used when you need to specify a type, but you don't need access to its specific properties.

On the other hand, reified type parameters allow you to access the type information at runtime. When a type parameter is marked as reified, it means that the actual type information is retained at runtime and can be used for reflection operations. Reified type parameters can be used as type arguments directly, and you can access their properties or perform type checks on them.

Reified type parameters are particularly useful in scenarios where you need to perform operations or checks based on the actual type information at runtime, such as checking if a type parameter is a specific type or invoking specific methods based on its type.

To mark a type parameter as reified, you need to use the reified keyword in the function declaration. For example:

inline fun doSomething(item: T) { // Access type information of T at runtime if (item is List<*>) { ... } }

In the above example, the T type parameter is marked as reified, allowing you to perform type checks using is operator or access methods specific to T.

It's worth noting that reified type parameters can only be used with inline functions since the code for inline functions is copied into the call site, retaining type information.

How to call a generic function in Kotlin?

To call a generic function in Kotlin, you can follow these steps:

  1. Define a function with a generic type parameter(s):

fun myGenericFunction(input: T) { // function body }

Here, T represents the generic type parameter.

  1. Call the generic function, specifying the actual type(s) for the generic parameter(s) inside angle brackets:

myGenericFunction("Hello")

In this example, String is provided as the actual type for the generic parameter T.

The Kotlin compiler can usually infer the generic type automatically based on the function arguments. So, you can also omit the explicit type specification:

myGenericFunction("Hello") // Compiler infers the type String

Additionally, if the generic function has multiple type parameters, you can specify all the types inside the angle brackets:

fun <T, U> myGenericFunction(input1: T, input2: U) { // function body }

myGenericFunction<String, Int>("Hello", 42)

Remember to replace the function name (myGenericFunction) and the generic type placeholder (T) with your own function and type names.

How to create a generic function with default type arguments in Kotlin?

To create a generic function with default type arguments in Kotlin, you can use the angle bracket notation to declare the type parameter, followed by a colon and the default type argument. Here's an example:

fun <T: Number, R: Any> processList(list: List, defaultValue: R = ""): List { val resultList = mutableListOf()

for (item in list) {
    // Process each item and add it to the result list
    val processedItem: R = item.toString() as R
    resultList.add(processedItem)
}

return resultList

}

In this example, we have a generic function processList that takes a list of T elements, where T is a type parameter that is constrained to be a Number. The function also takes a second parameter defaultValue of type R, which is a type parameter constrained to be any non-null type (Any).

The function processes each item of the input list, converts it to a string using the toString() method, and adds it to the result list. If the defaultValue parameter is not provided, the default value of an empty string is used. The function then returns the result list of type List<R>, where R is the type argument provided by the caller or the default type argument.