Streams have side effect on your collection (was: Re: Very strange bug on Streams and probably compiler)

Damien Cassou damien.cassou at laposte.net
Thu Feb 15 08:14:50 UTC 2007


Hi Klaus,


Klaus D. Witzel wrote:
> 
>> I really don't want to depend on the implementation. And in my
>> opinion, this is not encapsulated because this is MY String, not a
>> String created internally.
> 
> Not really. If you pass a boxed object (other than a SmallInteger) the  
> recipient can #become: it to anything he/she likes. This is reality.
> 

I'm not talking about security, only unconsistent side effect.


Klaus D. Witzel wrote:
> 
>> What I see is that the String I give to
>> the new Stream is modified. Then at a moment, the String does not
>> reflect the stream anymore. This doesn't sound coherent to me.
> 
> It is not coherent because you passed an explicitly written *constant*  
> which, in other languages, is believed to be immutable.
> 
>> And if
>> you all agree to the current behavior, then a documentation should be
>> written: "Don't use the collection after having created a stream on
>> it !"
> 
> Easier: don't pass constant collections to the streamers :)
> 

It's not about constant collections at all here.

myCollection := String new: 3.
myStream := WriteStream on: myCollection.
myStream nextPutAll: 'abcd' copy.

Here, myCollection is left untouched which sounds normal (it's still an
empty string of size 3). Now, lets replace #nextPutAll: by 4 #nextPut:

myCollection := String new: 3.
myStream := WriteStream on: myCollection.
myStream nextPut: $a; nextPut: $b; nextPut: $c; nextPut: $d.

This should have exactly the same behavior... however myCollection now
equals 'abc' !!! Why the first 3 characters ? Why not everything or nothing
at all ? This is why I think it's not coherent.

I've read the source code and I understand why it happens but I don't think
it's coherent.
And this as nothing to do with literals nor with immutability. This is a
completely different problem (this is why I changed the thread title).
-- 
View this message in context: http://www.nabble.com/Very-strange-bug-on-Streams-and-probably-compiler-tf3227838.html#a8981366
Sent from the Squeak - Dev mailing list archive at Nabble.com.




More information about the Squeak-dev mailing list