Skip to content
Commitwise
Menu

Type inference

The compiler works out a variable's type from the value you give it, so you don't have to write it.

Definition

With type inference, let count = 3 in Swift, val count = 3 in Kotlin and var count = 3 in Java all create an integer without spelling out Int or int. The variable is still strictly typed: you just didn't have to write the type. You can always add it when it makes the code clearer.

In practice

val names = listOf("Ana", "Marko") is inferred as List<String>, so names.add(42) is a compile error.

Related terms

Esc

Type to search the whole site.