in Number means " Number or its supertype." Int not a " Number or its supertype", it is its subtype.
In simple words, you stated that your addAnotherInt() wants a list that is at least generic, like receiving any type of Number .
Unlike addInt , item: T and list: MutableList<in T> declared. T itself is declared as a free type variable for a function, which means that it will be bound to each specific call site. Therefore when you say
addInt(4, mutableIntList)
Kotlin associates T with Int based on the first argument and passes it to the second argument, which is now MutableList<in Int> . You passed in a MutableList<Int> , which is compatible with this type, so Kotlin is satisfied.
If you announced
val mutableIntList: MutableList<Number> = mutableListOf(1, 2, 3)
then the code will be compiled, because now the list is general, as required, and you can add any Number to it.
source share