article banner

Exercise: Usage of generic types

The code below will not compile due to a type mismatch. Which lines will show compilation errors?

fun takeIntList(list: List<Int>) {} takeIntList(listOf<Any>()) takeIntList(listOf<Nothing>()) fun takeIntMutableList(list: MutableList<Int>) {} takeIntMutableList(mutableListOf<Any>()) takeIntMutableList(mutableListOf<Nothing>()) 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>()) 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>()) takeBoxOutNothing(BoxOut<Number>()) 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>()) takeBoxInInt(BoxIn<Any>())

Once you are done with the exercise, you can check your solution here.