[ANSI-Smalltalk] Behaviour of #collect:
Richard A. O'Keefe
ok at cs.otago.ac.nz
Mon Sep 22 07:13:26 BST 2008
Pretty much every Smalltalk I've tried has failed this test:
'abc' collect: [:each | each codePoint]
(or the equivalent using some non-standard replacement for
#codePoint). It seems obvious to me that this should make
a "best effort" attempt to collect the results of the block,
just as
(97 to: 99) collect: [:each | Character codePoint: each]
doesn't complain that an Interval can't store characters.
I have on occasion reported this as a bug and provided code to
make it work more or less as expected.
My own library has in each collection class a class method
SomeCollection withAll: aCollection collect: aBlock
so that I could hack this particular example using
Array withAll: 'abc' collect: [:each | each codePoint]
or even
ByteArray withAll: 'abc'
collect: [:each | each codePoint].
Would adding #withAll:collect: to every class that has #withAll:
be considered a good move?
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?
By the way, this particular example was not one constructed to
break things; it's a simplified version of something that came up
in real code.
More information about the ANSI-Smalltalk
mailing list