[squeak-dev] #adoptInstance: vs. Morph -> BorderedMorph

Levente Uzonyi leves at caesar.elte.hu
Thu Nov 21 10:07:59 UTC 2019


Hi Marcel,

On Thu, 21 Nov 2019, Marcel Taeumel wrote:

> Hi, all.
> 
> Why isn't the following code working?
> 
> BorderedMorph adoptInstance: Morph new.
> 
> In theory, I would expect all additional instance variables (borderWidth and borderColor) to be "nil".

BorderedMorph doesn't have the same format as Morph: it has two more 
instance variables.

The method comment says:
"Change the class of the argument anInstance into the receiver, provided
that the format of the receiver matches the format of the argument's class."

So extra slots are not tolerated. I think the primitive simply changes 
the class field of the object, so their formats must match to avoid VM 
crashes.

Perhaps you want to do is something like this instead:

changeClass := [ :instance :newClass |
 	| newInstance |
 	newInstance := newClass basicNew.
 	newInstance copyFrom: instance.
 	instance becomeForward: newInstance.
 	instance ].

changeClass value: Morph new value: BorderedMorph


Levente

> 
> The scenario is just workspace scripting. Trying something out. Setting up a scene, evaluating some code. Iterating quickly.
> 
> Best,
> Marcel
> 
>


More information about the Squeak-dev mailing list