#adoptInstance: vs. Morph -> BorderedMorph

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

#adoptInstance: vs. Morph -> BorderedMorph

marcel.taeumel
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".

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

Best,
Marcel


Reply | Threaded
Open this post in threaded view
|

Re: #adoptInstance: vs. Morph -> BorderedMorph

Levente Uzonyi
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
>
>