When using mutableListOf() or any collections object ; If without using the generics we initialise it with different data types then it starts behaving as a mutable list ? But if I mention generics (<Any> in this case) explicitly only then it works why ? for ex: val list = mutableListOf(1,2,"hIM"); --> this will behave as a mutable list and will not be able add anything to the list. whereas val list = mutableListOf <Any> (1,2,"him"); --> This just works fine and work as mutable list .
v
Vampire
10/31/2020, 10:57 AM
Well, it seems the type inference algorithm does not produce a union type for the type argument like
List<Int|String>
but instead does
List<Nothing>
, which makes it impossible to add anything. Probably because it is safer than to allow
Any
just because two types were used. You should open an issue about the type inference, that it should produce the union type instead imho. If there is none yet that is.