- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why can't I update a view of a buffer?
Tue, 2011-08-02, 22:46
http://www.scala-lang.org/docu/files/collections-api/collections_42.html
describes views of mutable sequences.
"This gives a view subarr which refers to the elements at positions 3
through 5 of the array arr. The view does not copy these elements, it
just provides a reference to them. Now, assume you have a method that
modifies some elements of a sequence. For instance, the following
negate method would negate all elements of the sequence of integers
it's given: "
Is the following a bug? An oversight? Or am I missing something?
scala> val a = (1 to 10).toArray
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> val a2 = a.view(6, 10)
a2: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqViewS(...)
scala> val a2 = a.view(5, 10)
a2: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqViewS(...)
scala> a2(0) = 42
scala> a
res58: Array[Int] = Array(1, 2, 3, 4, 5, 42, 7, 8, 9, 10)
scala> val b = (1 to 10).toBuffer
b: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 4, 5,
6, 7, 8, 9, 10)
scala> val b2 = b.view(6, 10)
b2: scala.collection.SeqView[Int,scala.collection.mutable.Buffer[Int]]
= SeqViewS(...)
scala> b2(0) = 42
:13: error: value update is not a member of
scala.collection.SeqView[Int,scala.collection.mutable.Buffer[Int]]
I mean, why doesn't view(from, to) return a mutable IndexedSeqView for
a mutable buffer?
Thanks,
Cay
Wed, 2011-08-03, 06:37
#2
Re: Why can't I update a view of a buffer?
>> scala> val b = (1 to 10).toBuffer
>> b: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 4, 5,
>> 6, 7, 8, 9, 10)
>>
>> scala> val b2 = b.view(6, 10)
>> b2: scala.collection.SeqView[Int,scala.collection.mutable.Buffer[Int]]
>> = SeqViewS(...)
>>
>> scala> b2(0) = 42
>> :13: error: value update is not a member of
>> scala.collection.SeqView[Int,scala.collection.mutable.Buffer[Int]]
>>
>> I mean, why doesn't view(from, to) return a mutable IndexedSeqView for
>> a mutable buffer?
>
> Because Buffer is not, in fact, an IndexedSeq -- ListBuffer, for
> instance, doesn't have that characteristic. ArrayBuffer does, and its
> view can be changed accordingly.
Thanks! I was mislead by the fact that b is in fact an ArrayBuffer. If you have
scala> val c = collection.mutable.ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
c: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4,
5, 6, 7, 8, 9, 10)
then it's as I expected:
scala> val c2 = c.view(5, 10)
c2: scala.collection.mutable.IndexedSeqView[Int,scala.collection.mutable.ArrayBuffer[Int]]
= SeqViewS(...)
scala> c2(0) = 42
This is an interesting twist that might come as a surprise to Java
programmers who are used to ignoring variable types.
Cheers,
Cay
On Tue, Aug 2, 2011 at 18:46, Cay Horstmann wrote:
> http://www.scala-lang.org/docu/files/collections-api/collections_42.html
> describes views of mutable sequences.
>
> "This gives a view subarr which refers to the elements at positions 3
> through 5 of the array arr. The view does not copy these elements, it
> just provides a reference to them. Now, assume you have a method that
> modifies some elements of a sequence. For instance, the following
> negate method would negate all elements of the sequence of integers
> it's given: "
>
> Is the following a bug? An oversight? Or am I missing something?
>
> scala> val a = (1 to 10).toArray
> a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>
> scala> val a2 = a.view(6, 10)
> a2: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqViewS(...)
>
> scala> val a2 = a.view(5, 10)
> a2: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqViewS(...)
>
> scala> a2(0) = 42
>
> scala> a
> res58: Array[Int] = Array(1, 2, 3, 4, 5, 42, 7, 8, 9, 10)
>
> scala> val b = (1 to 10).toBuffer
> b: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 4, 5,
> 6, 7, 8, 9, 10)
>
> scala> val b2 = b.view(6, 10)
> b2: scala.collection.SeqView[Int,scala.collection.mutable.Buffer[Int]]
> = SeqViewS(...)
>
> scala> b2(0) = 42
> :13: error: value update is not a member of
> scala.collection.SeqView[Int,scala.collection.mutable.Buffer[Int]]
>
> I mean, why doesn't view(from, to) return a mutable IndexedSeqView for
> a mutable buffer?
Because Buffer is not, in fact, an IndexedSeq -- ListBuffer, for
instance, doesn't have that characteristic. ArrayBuffer does, and its
view can be changed accordingly.