t
why?
c
Because extension functions are resolved statically
t
in what scenario would that function (for
MutableList
) lead to an error? Whether I declare a
MutableList<Int>
and concat it with a
MutableList<Double>
to get a
MutableList<Number>
or declare it as a
MutableList<Number>
initialized with only `Int`s, then insert a bunch of `Double`s doesn't make a difference
a
If you concat
MutableList<Int>
and
MutableList<Double>
to get a
MutableList<Number>
, what do you expect to happen when you add a
BigDecimal
to it?
(well, assuming you had a definition for what adding to a concatenated mutable list would do if the types did line up)
t
I still get a
MutableList<Number>
since
BigDecimal
is a
Number
. The list only stores references and the generic type is erased, so in fact I can put any object in it, as long as I don't declare it as a `MutableList<Int>`:
val list1: MutableList<Number> = MutableList<Int>(1,2,3,4,5).concat(MutableList<Double>(1.1, 2.2, 3.3)) // ok
Copy code
val list2 = MutableList<Int>(1,2,3,4,5) // ok
(list2 as MutableList<Double>) += 1.1 // ok
list2.map(::functionForInts) // not ok
a
this only works because you happen to be using a MutableList implementation that doesn't check the runtime type of its contents --
(list2 as MutableList<Double>) += 1.1
is not really ok, it doesn't make sense, just the runtime is deficient and allows it.