Search⌘ K
AI Features

Sensible Warnings

Explore how Kotlin’s compiler provides sensible warnings for unused parameters to help you identify potential code issues early. Understand the importance of treating warnings as errors using the -Werror option, and discover Kotlin’s context-aware approach to warnings, especially in the main() function. This lesson helps you write cleaner, more maintainable Kotlin code by proactively managing compiler warnings.

Getting ahead of errors

Even if a piece of code is valid syntactically, some potential problems may be lurking. Getting an early warning, during compilation time, can help us to proactively fix such possible issues. The Kotlin compiler looks out for quite a few potential issues in code.

For example, if a parameter that’s received in a function or a method isn’t used, then the compiler will give a warning. In the following script, the parameter passed to compute() isn’t used.

Kotlin
fun compute(n: Int) = 0
println(compute(4))

When you run this script, in addition to displaying the result, Kotlin ...