[squeak-dev] PositionableStream >> #peekBack behavior
Levente Uzonyi
leves at caesar.elte.hu
Thu Aug 6 23:43:06 UTC 2020
Hi Eric,
On Thu, 6 Aug 2020, Eric Gade wrote:
> Hello,
>
> What is the expected behavior for a stream that is at position 1 when you send #peekBack? Intuitively I'd expect to get the first element of the underlying collection as a response. But instead I am getting nil.
>
> Here is an example that is failing:
> ```
> elem := #(1 2 3 4).
> stream := ReadStream on: elem.
> first := stream next.
> first = 1.
> back := stream peekBack.
> back = first.
> ```
> The last message is currently responding false. In Pharo this returns true, so there are differences.
>
> The currently implementation of PositionableStream >> #peekBack is:
> ```
> peekBack
> "Return the element at the previous position, without changing position. Use indirect messages in case self is a StandardFileStream."
>
> | element |
> self position = 0 ifTrue: [self errorCantGoBack].
> self position = 1 ifTrue: [self position: 0. ^ nil].
> self skip: -2.
> element := self next.
> self skip: 1.
> ^ element
> ```
>
> I can see the nil being returned there explicitly, so that's "where it's happening." Should this be the case though?
The previous, probably original implmenetation of #peekBack used to send
#oldBack to the stream.
IIRC #oldBack was based on the idea that the position of a stream is an
index of a sequence where #next means +1 to that index while #back means
-1 to that index. Following that logic, you have to #skip: -2 and send
#next to get the element -1 to position.
#oldBack has been removed but the behavior of #peekBack is presumably the
same as it was before. Some ancient but now external code may rely on
#peekBack but it's not very likely such code would work in the current
Trunk.
#peekBack has no real users in the Trunk only a test remembers what it
used to do.
So, I think it's a good time to change its behavior to be based on #back.
Levente
>
> Thanks
>
>
> --
> Eric
>
>
More information about the Squeak-dev
mailing list
|