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:[email protected]] 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.
!!!*************************************************************************************
"Ce message et les pièces jointes sont confidentiels et réservés à l'usage
exclusif de ses destinataires. Il peut également être protégé par le secret
professionnel. Si vous recevez ce message par erreur, merci d'en avertir
immédiatement l'expéditeur et de le détruire. L'intégrité du message ne pouvant
être assurée sur Internet, la responsabilité de Worldline ne pourra être
recherchée quant au contenu de ce message. Bien que les meilleurs efforts
soient faits pour maintenir cette transmission exempte de tout virus,
l'expéditeur ne donne aucune garantie à cet égard et sa responsabilité ne
saurait être recherchée pour tout dommage résultant d'un virus transmis.
This e-mail and the documents attached are confidential and intended solely for
the addressee; it may also be privileged. If you receive this e-mail in error,
please notify the sender immediately and destroy it. As its integrity cannot be
secured on the Internet, the Worldline liability cannot be triggered for the
message content. Although the sender endeavours to maintain a computer
virus-free network, the sender does not warrant that this transmission is
virus-free and will not be liable for any damages resulting from any virus
transmitted.!!!"