Why does += for MutableList behave differently fro...
# announcements
y
Why does += for MutableList behave differently from = with +? It's a bit weird and also against with liskov substitution because for List += works exactly like = with +
m
What's the difference in behavour? No new list is created?
k
+=
doesn't work on
val list:List<T>
, but does work with
val mutableList:MutableList<T>
. It also works on
var list:List<T>
but IIRC on
var mutableList<T>
it doesn't work because it is ambiguous. AFAIK liskov substitution principle isn't violated.
b
It still is a bit strange that
val mutableList += a
adds a, while
var mutableList = mutableList + a
creates a copy. It feels like
+
and
+=
do 2 different things (and they do). In this special case
+=
is not implemented as
a = a + b
whcih I think is what is meant by @yawkat. But as you pointed out
+=
is not vallid on mutable lists that are assigned to a
var
so this is not really a problem.
After looking up liskov’s substitution principle(https://en.wikipedia.org/wiki/Liskov_substitution_principle) I can confidently say it’s not violated here. Both
plus
and
plusAssign
are implemented as extension functions on
List
and
MutableList
so they are called statically. This means that substitution like in liskov’s case is not an issue.
y
liskov substitution also applies to static type changes
k
a += b
is always equivalent with
a = a+b
. but if both assignPlus and
plus
have been defined on a, there is ambiguity.
liskov would not apply here indeed cause they are extension functions.
you wan't liskov on a subtype so that if it is cast to a more general type, the contract of that type still applies
y
why? if i replace the static type of the variable with a subtype, the behavior changes
thats a liskov substitution violation imo
k
No, in this case it's 2 different functions that happen to have the same name. It's like saying this violates liskov cause
hi(String)
returns a
String
which doesn't extend
Pair<String,Any>
Copy code
fun hi(name:Any) = "hi" to name
fun hi(name:String) = "hi, $name!"
y
i know that, but this is an item in effective java
41 apparently. overloaded methods with similar types should always do the same thing
i always thought that was a part of liskov
k
no, liskov is about subtypes following the method contract of the supertype
👍 1
k
It's a bit unfortunate, yes. I can't imagine ever wanting
+=
to mean "create a new list with an extra element".
y
eh, that's the sane behavior, because thats exactly what + does