yawkat
11/16/2019, 10:22 AMMarko Mitic
11/16/2019, 10:35 AMKroppeb
11/16/2019, 10:43 AM+=
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.Burkhard
11/16/2019, 1:10 PMval 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.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.yawkat
11/16/2019, 1:15 PMKroppeb
11/16/2019, 1:15 PMa += b
is always equivalent with a = a+b
. but if both assignPlus and plus
have been defined on a, there is ambiguity.yawkat
11/16/2019, 1:16 PMKroppeb
11/16/2019, 1:20 PMhi(String)
returns a String
which doesn't extend Pair<String,Any>
fun hi(name:Any) = "hi" to name
fun hi(name:String) = "hi, $name!"
yawkat
11/16/2019, 1:34 PMKroppeb
11/16/2019, 1:36 PMkarelpeeters
11/16/2019, 5:48 PM+=
to mean "create a new list with an extra element".yawkat
11/16/2019, 8:14 PM