scala - Cannot modify Seq.head -
i'm learning scala, , got confused seq.head.
scala> x = array(1, 2, 3) x: array[int] = [i@545e9f15 scala> x res64: array[int] = array(1, 2, 3) scala> x(0) res65: int = 1 scala> x.head res66: int = 1 scala> x(0) += 1 scala> x.head += 1 <console>:13: error: value += not member of int x.head += 1 ^ scala> x.head = 1 <console>:12: error: value head_= not member of scala.collection.mutable.arrayops[int] x.head = 1 ^ scala> seems there's implicit conventions happening beneath.
but scala api, type of array.head int (in case):
def head: t so why can modify element?
the key difference in question between x(0) += 1 (which works), , x.head += 1 (which fails).
x(0) += 1 equivalent x(0) = x(0) + 1, syntaxic sugar x.update(0, x.apply(0) + 1). instruction increases value of x(0).
x.head returns , int, immutable, x.head += 1 fails.
wiki
Comments
Post a Comment