Solution: Usage of generic types
The lines that fail are marked with X
:
fun takeIntList(list: List<Int>) {}
takeIntList(listOf<Any>()) X
takeIntList(listOf<Nothing>())
fun takeIntMutableList(list: MutableList<Int>) {}
takeIntMutableList(mutableListOf<Any>()) X
takeIntMutableList(mutableListOf<Nothing>()) X
fun takeAnyList(list: List<Any>) {}
takeAnyList(listOf<Int>())
takeAnyList(listOf<Nothing>())
class BoxOut<out T>
fun takeBoxOutInt(box: BoxOut<Int>) {}
takeBoxOutInt(BoxOut<Int>())
takeBoxOutInt(BoxOut<Number>()) X
takeBoxOutInt(BoxOut<Nothing>())
fun takeBoxOutNumber(box: BoxOut<Number>) {}
takeBoxOutNumber(BoxOut<Int>())
takeBoxOutNumber(BoxOut<Number>())
takeBoxOutNumber(BoxOut<Nothing>())
fun takeBoxOutNothing(box: BoxOut<Nothing>) {}
takeBoxOutNothing(BoxOut<Int>()) X
takeBoxOutNothing(BoxOut<Number>()) X
takeBoxOutNothing(BoxOut<Nothing>())
fun takeBoxOutStar(box: BoxOut<*>) {}
takeBoxOutStar(BoxOut<Int>())
takeBoxOutStar(BoxOut<Number>())
takeBoxOutStar(BoxOut<Nothing>())
class BoxIn<in T>
fun takeBoxInInt(box: BoxIn<Int>) {}
takeBoxInInt(BoxIn<Int>())
takeBoxInInt(BoxIn<Number>())
takeBoxInInt(BoxIn<Nothing>()) X
takeBoxInInt(BoxIn<Any>())
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.