I managed to do it :) 

What i did not say is that the subclasses (with new inst var) i want to
use are anonymous subclasses. 

So i subclassed the SlotClassBuilder and i did: 

- generate an anonymous subclass from the source class 

- migrate my object 

- modify the anonymous subclass with new instance variables 

- rebuild the class with my own SlotClassBuilder 

It seems to work. I did not try how to migrate back my object to its
original class but i think i might have a problem there. Also it seems
very slow. 

Indeed it is a very certain, specific application :) 

Steven. 

Le 2017-06-07 15:32, Blondeau Vincent a écrit :

> So, I do not think that is possible... 
> 
> Any solution I see is either to change the superclass or to add an external 
> object with a mapping between your new objects and the values of your new 
> IVs. 
> 
> Or you do the migration and after you add the IV once all instances are 
> migrated. 
> 
> But, I repeat that, as written in comments of primitiveChangeClassTo,  "The 
> facility is really provided for certain, very specific applications (mostly 
> related to classes changing shape) and not for casual use." 
> 
> Vincent 
> 
> DE : Pharo-users [mailto:pharo-users-boun...@lists.pharo.org] DE LA PART DE 
> Steven Costiou
> ENVOYÉ : mercredi 7 juin 2017 15:18
> À : Any question about pharo is welcome
> OBJET : Re: [Pharo-users] How to migrate an object to a subclass of its class 
> but with new inst vars ? 
> 
> Hi Vincent, thanks for your quick answer. 
> 
> This is the default behavior of adoptInstance: which calls 
> primitiveChangeClassTo: 
> 
> But i specifically want to gain new inst vars, which does not work that way. 
> 
> I can't add an instvar to the superclass since i don't want to change my 
> system. 
> 
> Steven. 
> 
> Le 2017-06-07 15:09, Blondeau Vincent a écrit :
> 
>> 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-boun...@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.
 

Reply via email to