Skip to main content
Type inference is a compiler feature in Kotlin that automatically deduces the static type of a variable, property, function return, or generic argument from its context and initialization expression at compile time. This mechanism allows developers to omit explicit type declarations while maintaining the strict type safety of a statically typed language.

Variable and Property Declarations

When declaring a variable using the val (read-only) or var (mutable) keywords, the Kotlin compiler analyzes the right-hand side of the assignment. The type of the evaluated expression becomes the permanent static type. While local variables require an immediate initialization expression for type inference, class-level or top-level properties can also infer their type from a custom getter’s return value without an explicit initializer.

Single-Expression Functions

The compiler can infer the return type of a function if it is written as a single expression using the assignment operator (=) instead of a block body. The inferred return type matches the static type of the evaluated expression.

Generic Type Arguments

Kotlin infers generic type parameters based on the arguments provided to a function or constructor. If the type can be deduced from the passed values, explicit type arguments inside angle brackets (<T>) can be omitted.

Lambda Parameters

When a lambda expression is passed to a higher-order function, the compiler infers the types of the lambda parameters based on the expected functional interface or the function signature.

Static Type Immutability

Kotlin remains strictly statically typed regardless of inference. Once the compiler infers a type, it is permanently bound to that identifier within its scope. Attempting to reassign a var to a value of a non-conforming type results in a Type mismatch compilation error.

Literal Type Resolution

The compiler applies specific rules when inferring types from numeric literals:
  • Integers: Whole numbers are inferred as Int by default. If the value exceeds Int.MAX_VALUE, it is automatically inferred as Long.
  • Longs: Explicitly appending an L suffix forces inference to Long.
  • Floating-point: Decimal numbers are inferred as Double by default.
  • Floats: Explicitly appending an f or F suffix forces inference to Float.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More