Quick Answer

Inferred type doesn't match expected type

Understanding the Issue

Kotlin is statically typed and enforces type safety. This error occurs when the compiler detects incompatible types, often requiring explicit conversion or type adjustment.

The Problem

This code demonstrates the issue:

Kotlin Error
// Problem: Type mismatch
fun process(numbers: List<Int>) { ... }
process(listOf("1", "2")) // Error

The Solution

Here's the corrected code:

Kotlin Fixed
// Solution 1: Correct type
process(listOf(1, 2))

// Solution 2: Convert types
process(listOf("1", "2").map { it.toInt() })

// Solution 3: Change function signature
fun process(strings: List<String>) { ... }

Key Takeaways

Ensure types match or provide explicit conversions when needed.