Kotlin Inline & Reified Functions

Kotlin Tutorial

Kotlin Inline & Reified Functions – Complete Beginner Guide

Kotlin provides powerful features that improve performance and make code more expressive. Two such advanced yet extremely useful features are inline functions and reified type parameters.

If you’re learning Kotlin — especially for Android or backend development — understanding these concepts will help you:

  • Improve performance

  • Avoid unnecessary object creation

  • Work with generics more effectively

  • Write cleaner and more flexible code

In this beginner-friendly guide, you will learn:

  • What inline functions are

  • Why inline functions exist

  • What reified type parameters are

  • Why reified requires inline

  • Real-world examples

  • Performance benefits

  • Common mistakes

  • Best practices

Let’s dive in


What Is an Inline Function in Kotlin?

An inline function is a function whose code is copied directly at the call site during compilation instead of being called normally.

In simple terms:

Instead of creating a function call, Kotlin replaces it with the function’s body.


Why Do We Need Inline Functions?

Inline functions are mainly used to:

  •  Reduce overhead of lambda expressions
  •  Avoid extra object creation
  •  Improve performance
  •  Enable reified type parameters

When you pass a lambda to a normal function, Kotlin creates an object. Inline eliminates this overhead.


Basic Example of Inline Function

Without inline:

With inline:

Now the lambda is inserted directly at the call site.


How Inline Works Internally

Normal function:

Internally:

  • Lambda object created

  • Function call executed

Inline function:

  • Lambda code directly inserted

  • No extra object created

Better performance.


Example – Performance Optimization


 

Inline reduces overhead for small, frequently called functions.


What Is a Reified Type in Kotlin?

Normally, generic type information is erased at runtime (Type Erasure).

Example:

You cannot access T::class.


Why? Because of Type Erasure

At runtime, generic types are removed. The JVM does not know the actual type of T.

This is where reified comes in.


Reified Type Parameter in Kotlin

Reified allows you to access the actual type inside a generic function.

Important:

Reified can only be used inside inline functions.


Basic Example of Reified


 

Now T is available at runtime.


Why Reified Requires Inline

Reified works because:

  • Inline copies function code

  • Type information becomes available

  • Compiler knows actual type

Without inline, reified cannot work.


Real-World Example – Safe Cast

Without reified:

With reified:

Usage:

Cleaner and safer.


Real-World Example – Gson Parsing

Instead of:

With reified:

Usage:

More readable.


noinline and crossinline Keywords

When using inline, you can control lambda behavior.


noinline

Prevents lambda from being inlined.


crossinline

Prevents non-local return.

Advanced usage.


Inline vs Normal Function

FeatureNormal FunctionInline Function
Lambda object createdYesNo
Performance overheadHigherLower
Supports reifiedNoYes
Best forLarge functionsSmall functions

Use inline wisely.


When Should You Use Inline?

Use inline when:

  • Function takes lambda

  • Function is small

  • Called frequently

  • Performance matters

Avoid inline for:

  • Large functions

  • Complex logic

  • Rarely called functions


When Should You Use Reified?

Use reified when:

  • Working with generics

  • Need runtime type info

  • Building helper functions

  • Parsing JSON

  • Type-safe casting


Common Beginner Mistakes

 Overusing Inline

Inlining large functions increases bytecode size.


Forgetting Inline for Reified

Reified requires inline.


 Misusing crossinline

Use only when needed.


Ignoring Performance Trade-offs

Inline increases code size.


Performance Considerations

Inline:

  • Removes function call overhead

  • Removes lambda object creation

  • Improves performance in small functions

But:

  • Increases bytecode size

  • May increase APK size

Balance is important.


Inline in Higher-Order Functions

Many Kotlin standard library functions are inline:

  • let

  • run

  • apply

  • also

  • forEach

This improves performance of lambda-heavy code.


Frequently Asked Questions (FAQs)

1. What is an inline function in Kotlin?

An inline function copies its body at the call site to reduce overhead.

2. What is reified in Kotlin?

Reified allows access to generic type information at runtime.

3. Why does reified require inline?

Because type information becomes available when function is inlined.

4. When should I use inline?

Use inline for small higher-order functions.

5. Does inline improve performance?

Yes, by removing lambda object creation and function call overhead.


Conclusion

Inline and reified functions are advanced but extremely powerful features in Kotlin.

You learned:

  • What inline does

  • Why it improves performance

  • How reified works

  • Real-world examples

  • Best practices

Mastering inline and reified will help you write more efficient and flexible Kotlin code.

You may also like...