[squeak-dev] Object >> #copyFrom: vs Object >> #copySameFrom:

Thiede, Christoph Christoph.Thiede at student.hpi.uni-potsdam.de
Fri Jan 3 20:16:12 UTC 2020

Hi Levente,

I don't get that.

Did I interpret your explanation correctly?

fooClass := Object newUniqueClassInstVars: 'a b' classInstVars: ''.
barClass := Object newUniqueClassInstVars: 'b c' classInstVars: ''.
foo := fooClass new.
bar := barClass new
instVarNamed: #b put: 2;
instVarNamed: #c put: 3;
foo copyFrom: bar.
foo instVarNamed: #b "expected: 2, actual: nil"

Otherwise, if I put #b as the first instvar in both classes, #copyFrom: and #copySameFrom: won't differ again ...



Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Levente Uzonyi <leves at caesar.elte.hu>
Gesendet: Freitag, 3. Januar 2020 19:05 Uhr
An: The general-purpose Squeak developers list
Betreff: Re: [squeak-dev] Object >> #copyFrom: vs Object >> #copySameFrom:

Hi Christoph,

The two methods are different. #copyFrom: copies variables by index while
#copySameFrom: copies variables by name.
So, if you have an object named foo of class Foo with 2 instance variables
a and b, and an object named bar of class Bar with 2 instance variables b
and c, then foo copyFrom: bar will copy bar's b to foo's a, and bar's c to
foo's b. #copySameFrom: will copy bar's b to foo's b and leave foo's a as


On Fri, 3 Jan 2020, Thiede, Christoph wrote:

> Hi all,
> I don't get the actual difference between #copyFrom: and #copySameFrom:.
> The latter looks more "modern" to me, as it uses a primitive and has several implementors.
> In my opinion, none of them actually matches its description ("Copy to myself all instance variables [named the same in | I have in common with] otherObject"). The following leaves o2 empty:
> c1 := Object newUniqueClassInstVars: 'foo bar' classInstVars: ''.
> c2 := Object newUniqueClassInstVars: 'bar foo' classInstVars: ''.
> o1 := c1 new
> instVarNamed: #foo put: 6;
> instVarNamed: #bar put: 7.
> o2 := o1 as: c2.
> o2 instVarAt: 1 "nil".
> o2 copySameFrom: o1.
> o2 instVarAt: 1 "nil".
> Question: Could we deprecate #copySameFrom:, and in #copyFrom:, copy *all* matching instvars without respecting their order? Or did I miss any intended difference in behavior?
> Best,
> Christoph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200103/7bd40c8d/attachment.html>

More information about the Squeak-dev mailing list