Hi,
You should be able to do it using the method primitiveChangeClassTo: but it not recommended to do it. Are you sure that it is the only way to resolve your problem?
If you really want to, an example:
Object subclass: #Toto
slots: { #tata }
classVariables: { }
category: 'Temp'.
Toto subclass: #Titi
slots: { }
classVariables: { }
category: 'Temp'.
obj := Toto new.
obj primitiveChangeClassTo: Titi new.
obj has a new class.
However, you cannot add a new instance variable this way but you keep the values of the instance.
But nothing avoid you to add an empty instance variable in a superclass that only the subclass will use.
I hope that it will help you
Vincent
De : Pharo-users [mailto:pharo-users-bounces@lists.pharo.org]
De la part de Steven Costiou
Envoy�� : mercredi 7 juin 2017 14:54
�� : Pharo users users
Objet : [Pharo-users] How to migrate an object to a subclass of its class but with new inst vars ?
Hi,
i'm trying to migrate an object from a given class A to a subclass of A with new inst vars. Can't get it to work.
I have a class and an instance of it, say:
Object subclass: #A
instanceVariableNames: ''
|a|
a := A new.
A has no instance variables. Now i create a subclass B of A with a new inst var:
A subclass: #B
instanceVariableNames: 'x'
I want to do the following:
B adoptInstance: a
It does'nt work, for what i understand it is because the format of the classes are different (B has an inst var and A has not).
How can i make the a object become an instance of B (subclass of a's class), making a acquiring the new inst var x (and in more complex cases keeping its own previous inst vars) ?
Steven.