How to migrate an object to a subclass of its class but with new inst vars ?

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

How to migrate an object to a subclass of its class but with new inst vars ?

Steven Costiou-2

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 | Threaded
Open this post in threaded view
|

Re: How to migrate an object to a subclass of its class but with new inst vars ?

Blondeau Vincent

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:[hidden email]] 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.!!!"
Reply | Threaded
Open this post in threaded view
|

Re: How to migrate an object to a subclass of its class but with new inst vars ?

Steven Costiou-2

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:[hidden email]] 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.!!!"

 

--
kloum.io
Reply | Threaded
Open this post in threaded view
|

Re: How to migrate an object to a subclass of its class but with new inst vars ?

Blondeau Vincent

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:[hidden email]] 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 [[hidden email]] 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.!!!"

 

--

kloum.io


!!!*************************************************************************************
"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.!!!"
Reply | Threaded
Open this post in threaded view
|

Re: How to migrate an object to a subclass of its class but with new inst vars ?

Steven Costiou-2

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:[hidden email]] 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 [[hidden email]] 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 | Threaded
Open this post in threaded view
|

Re: How to migrate an object to a subclass of its class but with new inst vars ?

Denis Kudriashov
In reply to this post by Steven Costiou-2

2017-06-07 14:53 GMT+02:00 Steven Costiou <[hidden email]>:

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).


Yes, #adopt checks class format. 
Use need to use #become to achieve what you want:
a becomeForward: B new 

Reply | Threaded
Open this post in threaded view
|

Re: How to migrate an object to a subclass of its class but with new inst vars ?

Steven Costiou-2

Yes but when you do that you loose all the states from "a" (at least the values). You have to do state migration management...

Le 2017-06-07 16:18, Denis Kudriashov a écrit :


2017-06-07 14:53 GMT+02:00 Steven Costiou <[hidden email]>:

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).


Yes, #adopt checks class format. 
Use need to use #become to achieve what you want:
a becomeForward: B new 
 

 

Reply | Threaded
Open this post in threaded view
|

Re: How to migrate an object to a subclass of its class but with new inst vars ?

Denis Kudriashov

2017-06-07 16:43 GMT+02:00 Steven Costiou <[hidden email]>:
Yes but when you do that you loose all the states from "a" (at least the values). You have to do state migration management...

You just need extra step to copy all state from old object:
b := B new.
b copyFrom: a.
a becomeForward: b.


Reply | Threaded
Open this post in threaded view
|

Re: How to migrate an object to a subclass of its class but with new inst vars ?

Steven Costiou-2

Hmm ok, but can i easily make b become a again later ? I guess i would just need to instanciate a new a...

 

But would it not be faster to just change the class (which finally works with a little tuning) ?

I can't test right now but i will compare the two things...

 

Le 2017-06-07 21:59, Denis Kudriashov a écrit :


2017-06-07 16:43 GMT+02:00 Steven Costiou <[hidden email]>:
Yes but when you do that you loose all the states from "a" (at least the values). You have to do state migration management...

You just need extra step to copy all state from old object:
b := B new.
b copyFrom: a.
a becomeForward: b.
 
Reply | Threaded
Open this post in threaded view
|

Re: How to migrate an object to a subclass of its class but with new inst vars ?

Denis Kudriashov

2017-06-07 22:30 GMT+02:00 Steven Costiou <[hidden email]>:
But would it not be faster to just change the class (which finally works with a little tuning) ?

In fact your solution with anonymous subclass is doing what I describe underhood.