[squeak-dev] Xtreams question - OC of chars to String

mkobetic at gmail.com mkobetic at gmail.com
Sun Dec 30 06:01:55 UTC 2012


"Chris Cunnington"<smalltalktelevision at gmail.com> wrote:
> I don't know. The situation is more like:
> 
>      oc := #($a $b $c).
>      oc := oc asOrderedCollection.
>      oc reading read: 3   an OrderedCollection($a $b $c)
> 
> 
> The terminal is the OC. But it returns an OC and I want a string from 
> the chars. I've been looking at the transforms page of the Xtreams docs 
> and that seems more about changing the encoding. I think my problem 
> wasn't really about Xtreams.

Hm, I have to admit that collection streams should perhaps support overriding their contentsSpecies. As it is they assume it should be the same as that of their underlying collection. One might argue that terminal streams should stick to their primary purpose of accessing terminals rather than trying to support some types of conversions as well. But there already is a precedent of sorts in the external streams, so it's not entirely solid argument either.

Barring that, you could write instead of reading, which would allow you to control the type of the terminal collection.

	String new writing write: 'abcdefg' asOrderedCollection; close; terminal

The reading equivalent would be using the longer version of read where you pass in the target collection but you're then forced to handle the collection size explicitly which seems less elegant:

	oc :=  'abcdefg' asOrderedCollection.
	string := String new: oc size.
	oc reading read: oc size into: string.

It feels sort of like doing the job of the write stream above by hand. You could also use an identity transform that allows setting the contents species:

	('abcdefg' asOrderedCollection reading collecting: [ :c | c ]) contentsSpecies: String; rest

But that again feels like more trouble than it's worth. I think that's about all that Xtreams have to offer here.

HTH,

Martin




More information about the Squeak-dev mailing list