[ANSI-Smalltalk] Behaviour of #collect:

cstb jas at cruzio.com
Mon Sep 22 20:48:02 BST 2008


At 11:13 PM 9/21/2008, Richard O'Keefe wrote:
>Pretty much every Smalltalk I've tried has failed this test:
>
>        'abc' collect: [:each | each codePoint]

..snip...


>        (97 to: 99) collect: [:each | Character codePoint: each]
>
>doesn't complain that an Interval can't store characters.

...snip...

>Would adding #withAll:collect: to every class that has #withAll:
>be considered a good move?


Seems backwards to me, and suggests an implementation with an
extraneous copy.  As opposed to

>>collect: a1block as: aCollectionClass
>>collect: a1block asKeyed: aKeyedCollectionClass

and/or

>>collect: a1block into: anExistingCollection
>>collect: a1block intoKeyed: existingKeyedCollection


>What exactly should #collect: do when sent to an object that
>belongs to a class whose instances cannot hold the results that
>the block returns?


Great way to phrase the question.

Some possible rules which strike me as sensible:

1) Same class as receiver, if possible, otherwise send error to self.
2) Same species as receiver, if possible, otherwise send error to self.
3) (a1block value: self first) collectionSpecies
4) always anArray
5) always anOrderedCollection
6) always aList
7) best effort, via some predictable sequence of retries
8) as is (essentially case-by-case ad-hoc)

I *think* I'd vote for #1, as a standard, even though it would likely break
some currently working code, because it seems most consistant/predictable.

Meanwhile, I'm using #collect:into:, as needed.


Regards,

-cstb 




More information about the ANSI-Smalltalk mailing list