Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange ArrayBuffer behavior

Can someone please explain to me why the padTo method of ArrayBuffer doesn't work as I would expect it to? In this example, I would expect the array created by toArray to have a length of 10.

scala> val b = new scala.collection.mutable.ArrayBuffer[Byte]
b: scala.collection.mutable.ArrayBuffer[Byte] = ArrayBuffer()

scala> b.append(2)

scala> b
res1: scala.collection.mutable.ArrayBuffer[Byte] = ArrayBuffer(2)

scala> b.append(2)

scala> b
res3: scala.collection.mutable.ArrayBuffer[Byte] = ArrayBuffer(2, 2)

scala> b.padTo(10,0)
res4: scala.collection.mutable.ArrayBuffer[AnyVal] = ArrayBuffer(2, 2, 0, 0, 0, 0, 0, 0, 0, 0)

scala> b.toArray
res5: Array[Byte] = Array(2, 2)
like image 769
Kim Stebel Avatar asked Dec 22 '22 03:12

Kim Stebel


1 Answers

Because padTo returns a new sequence (it doesn't mutate the existing one):

Try

var c = b.padTo(10,0)

c.toArray

See also: https://issues.scala-lang.org/browse/SI-2257

like image 109
Chris Shain Avatar answered Jan 02 '23 10:01

Chris Shain