[Pharo-users] some collection observations

Werner Kassens wkassens at libello.com
Wed Mar 5 10:28:50 EST 2014


Hi,

1. i do this in pharo 2.0:
b:=Bag newFrom: #(1 2).
OrderedCollection newFrom: b."works nicely"
Array newFrom: b."error"
the error happens because ArrayedCollection>>newFrom: inherited by Array 
expects a SequenceableCollection that has 'at:' implemented. but the 
first line of ArrayedCollection>>newFrom: says:
newFrom: aCollection
it would be more clear,if it would say:
newFrom: aSequenceableCollection

2. this also produces an error:
b as: Array.
because Object>>as: uses newFrom:. and since as: is a very general 
method, i'd guess that ArrayedCollection>>newFrom: should have an 
implementation that does not throw an exception. if changing a Bag to an 
OrderedCollection works, this should also work with an Array. or does 
this make much sense: <grin>
(b as: OrderedCollection) as:Array "this way it works"

3. withAll: does essentially the same as newFrom: and has the same 
problem with the argument naming. but Array>>withAll: is much much 
faster than Array>>newFrom:. try:
b:=Array new:100000.
[Array withAll: b]timeToRun.
[Array newFrom: b]timeToRun.
perhaps one could differentiate the two methods in such a way, that 
withAll: is the occasionally faster method that for some collections 
only accepts aSequenceableCollection as argument and newFrom: accepts 
any aCollection. This way also Object>>as: would work as expected. if 
one does not want to change this in my opinion slightly inconsistent 
behaviour, then <grin> it would make sense to implement
Array>>newFrom: aSequenceableCollection
^Array withAll: aSequenceableCollection.
simply for speed reasons. and of course a consistent argument naming 
would be nice.
werner




More information about the Pharo-users mailing list