private val magicNumbers = listOf(7, 13) fun name(a: Any?): String = when (a) { null -> "Nothing" 1, 2, 3 -> "Small number" in magicNumbers -> "Magic number" in 4..100 -> "Big number" is String -> "String: $a" is Int, is Long -> "Int or Long: $a" else -> "No idea, really" } fun main() { println(name(1)) // Small number println(name("A")) // String: A println(name(null)) // Nothing println(name(5)) // Big number // (because 5 is in 4..100) println(name(100)) // Big number // (because 100 is in 4..100) println(name('A')) // No idea, really // (because 'A' is Char) println(name("1")) // String: 1 println(name(-1)) // Int or Long: -1 println(name(101)) // Int or Long: 101 // (because 101 is greater than 100, so not in 4..100) println(name(1L)) // Int or Long: 1 // (because 1L is Long) println(name(7)) // Magic number // (because 7 is in magicNumbers collection) println(name(3)) // Small number println(name(3.0)) // No idea, really // (because 3.0 is Double) println(name(100L)) // Int or Long: 100 // (because 100L is Long) }
The author:
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.