Slide 9
Slide 9 text
Kotlinの List
MutableList インスタンスを List 変数に代入する:
>>> val xs: MutableList = mutableListOf(1, 2, 3)
// イミュータブル(?)とされるList型の変数に代入できてしまった
>>> val ys: List = xs
>>> ys.add(4)
error: unresolved reference: add
ys.add(4)
^
>>> xs.add(4)
res3: kotlin.Boolean = true
>>> xs
res4: kotlin.collections.MutableList = [1, 2, 3,
4]
// MutableList型の変数xsからの破壊的更新がysに必然的に波及している
>>> ys
res5: kotlin.collections.List = [1, 2, 3, 4]
9