Hi guys

I was looking at the Collection chapter and I stumbled upon newFrom: and I wonder what is the real
difference between newFrom: and withAll:.
I have the impression that there is not much difference. There are only 47 senders of newFrom: in the default Pharo image.

Dictionary class>>withAll: interprets its argument as a collection of values,
whereas Dictionary class>>newFrom: expects a collection of associations.

I would really deprecate newFrom: in the future.

Stef
I can understand using anObject newFrom: anotherObject

newFrom: aSimilarObject
"Create an object that has similar contents to aSimilarObject. If the classes have any instance variables with the same names, copy them across. If this is bad for a class, override this method."

    ^ (self isVariable
        ifTrue: [self basicNew: aSimilarObject basicSize]
        ifFalse: [self basicNew]) copySameFrom: aSimilarObject

copySameFrom: otherObject
    "Copy to myself all instance variables named the same in otherObject.
    This ignores otherObject's control over its own inst vars."

    | myInstVars otherInstVars |
    myInstVars := self class allInstVarNames.
    otherInstVars := otherObject class allInstVarNames.
    myInstVars doWithIndex: [:each :index | | match |
        (match := otherInstVars indexOf: each) > 0 ifTrue:
            [self instVarAt: index put: (otherObject instVarAt: match)]].
    1 to: (self basicSize min: otherObject basicSize) do: [:i |
        self basicAt: i put: (otherObject basicAt: i)].

but I do not see the point to use this protocol over withAll: for collection.

And in general I would prefer to call it cloneFrom:

Stef

Reply via email to