Re: Index 1 is out of bounds
- From: IanB <ranyart@xxxxxxxxxxxx>
- Date: Tue, 29 Jan 2008 19:17:28 +0000
Diego,
s := ReadStream on: #(3 4) asOrderedCollection.
s next: 1
I got an "Index 1 is out of bounds" error. May be it should be reported as
a bug.
Yes, that does look like a bug but, as Bruno said, I think that most
people tend to only stream over ArrayedCollection subclasses (Array
and String for example), and these work correctly. That would
probably explain why this hasn't been noticed before.
The problem stems from the way that OrderedCollections are created
when using #new:. The method comment states
"Answer a new instance of the receiver with sufficient initial
capacity to hold the number of elements specified by the <integer>
argument, count. The instance is initially empty (i.e.it reports 0
when sent #size)."
nb: "the instance is initially empty".
This means that
x := OrderedCollection new: 10.
x at: 5
will answer an error as x is created as a collection with a "capacity"
of 10 elements (i.e. it can accept 10 added elements before needing to
#grow itself) but which, initially, is empty.
The code you quoted creates a new OrderedCollection (in the
Stream>>next: method) and tries to copy elements from the stream's
collection into this new collection using #at:put: which, as we see
above, will not work.
One possible fix, but which might create performance issues, is to
change the Stream>>next: method to
^self contentsSpecies
withAll:
(self
next: anInteger
into: (Array new: anInteger)
startingAt: 1)
But, IMHO, sticking to ArrayedCollection subclasses is probably a
better bet.
--
Ian
The from address is valid
.
- Follow-Ups:
- Re: Index 1 is out of bounds
- From: Peter Kenny
- Re: Index 1 is out of bounds
- References:
- Index 1 is out of bounds
- From: DiegoC
- Index 1 is out of bounds
- Prev by Date: Re: Index 1 is out of bounds
- Next by Date: Re: Index 1 is out of bounds
- Previous by thread: Re: Index 1 is out of bounds
- Next by thread: Re: Index 1 is out of bounds
- Index(es):
Relevant Pages
|