Changing object class

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

Changing object class

Oleg Richards
Hello Guys!

Following question (maybe its a little bit "smalltalkish"):
I have an object - an instance of RowNumeric class. I also have 2  
classes derived from this class. Can i change instance class from one  
derived to another without touching instance variables.

So i have RowNumeric with all needed variables. And now i want to  
convert some numeric rows to total ones.

Cheers, Oleg
_______________________________________________
Seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

RE: Changing object class

Sebastian Sastre-2
Smalltalkish how we function so..

First of all it's really rare to have a real need of changing one object in
place of another so I'll convice myself about other simpler options like
instantiation.

Do you need to preserve identity of the objects or data is enough?

If data is enough you can make:

        SomeThing>>asOtherThing

                ^ OtherThing fromSomeThing: self


        OtherThing class>>fromSomeThing: aSomeThing

                ^ self new
                        prop1: aSomeThing prop1;
                        prop2: aSomeThing prop2;
                        ...
                        yourself

        that is enough?

Sebastian Sastre
PS: if identity is a must then you should investigate the #become: message


> -----Mensaje original-----
> De: [hidden email]
> [mailto:[hidden email]] En nombre
> de Oleg Richards
> Enviado el: Miércoles, 03 de Octubre de 2007 05:38
> Para: [hidden email]
> Asunto: [Seaside] Changing object class
>
> Hello Guys!
>
> Following question (maybe its a little bit "smalltalkish"):
> I have an object - an instance of RowNumeric class. I also
> have 2 classes derived from this class. Can i change instance
> class from one derived to another without touching instance variables.
>
> So i have RowNumeric with all needed variables. And now i
> want to convert some numeric rows to total ones.
>
> Cheers, Oleg
> _______________________________________________
> Seaside mailing list
> [hidden email]
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside

_______________________________________________
Seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Changing object class

Philippe Marschall
In reply to this post by Oleg Richards
2007/10/3, Oleg Richards <[hidden email]>:
> Hello Guys!
>
> Following question (maybe its a little bit "smalltalkish"):
> I have an object - an instance of RowNumeric class. I also have 2
> classes derived from this class. Can i change instance class from one
> derived to another without touching instance variables.

The question is not whether you can but whether you should. That can
be answered with no. Fix your design for example by use of delegation
for example by using the strategy pattern.

Cheers
Philippe

> So i have RowNumeric with all needed variables. And now i want to
> convert some numeric rows to total ones.
>
> Cheers, Oleg
> _______________________________________________
> Seaside mailing list
> [hidden email]
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
>
_______________________________________________
Seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Changing object class

Bert Freudenberg
In reply to this post by Sebastian Sastre-2
There is #as: which copies all instance variables of the same name as  
well as indexed variables to a new class. So you could say

numericRow := RowNumeric new.
...
totalRow := numericRow as: RowTotal.

As Sebastian wrote, this does not change the identity, you will have  
both the numericRow and totalRow objects afterwards.

Instead, you could really change the original row's identity:

row := RowNumeric new.
...
row becomeForward: (row as: RowTotal).

And as others have noted the need for doing this is a sign of bad  
design. Sometimes It's handy to be able to do this nonetheless ;)

- Bert -

On Oct 3, 2007, at 14:24 , Sebastian Sastre wrote:

> Smalltalkish how we function so..
>
> First of all it's really rare to have a real need of changing one  
> object in
> place of another so I'll convice myself about other simpler options  
> like
> instantiation.
>
> Do you need to preserve identity of the objects or data is enough?
>
> If data is enough you can make:
>
> SomeThing>>asOtherThing
>
> ^ OtherThing fromSomeThing: self
>
>
> OtherThing class>>fromSomeThing: aSomeThing
>
> ^ self new
> prop1: aSomeThing prop1;
> prop2: aSomeThing prop2;
> ...
> yourself
>
> that is enough?
>
> Sebastian Sastre
> PS: if identity is a must then you should investigate the #become:  
> message
>
>
>> -----Mensaje original-----
>> De: [hidden email]
>> [mailto:[hidden email]] En nombre
>> de Oleg Richards
>> Enviado el: Miércoles, 03 de Octubre de 2007 05:38
>> Para: [hidden email]
>> Asunto: [Seaside] Changing object class
>>
>> Hello Guys!
>>
>> Following question (maybe its a little bit "smalltalkish"):
>> I have an object - an instance of RowNumeric class. I also
>> have 2 classes derived from this class. Can i change instance
>> class from one derived to another without touching instance  
>> variables.
>>
>> So i have RowNumeric with all needed variables. And now i
>> want to convert some numeric rows to total ones.
>>
>> Cheers, Oleg


_______________________________________________
Seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Changing object class

Lukas Renggli
In reply to this post by Sebastian Sastre-2
> PS: if identity is a must then you should investigate the #become: message

Or rather #primitiveChangeClassTo:, but check out the comment in this method.

Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch
_______________________________________________
Seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Changing object class

Oleg Richards
In reply to this post by Oleg Richards
Hmmm.. Thank you guys!

How do you think i can change it:
  - I have a sheet with different kind of rows
  - each kind of row is represented by own class
  - i want to replace one row with another one type

I think that the best way is to delete old one and create identically  
same but as instance
of another class. Is this right way?

Cheers, Oleg
_______________________________________________
Seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

RE: Changing object class

Sebastian Sastre-2
> -----Mensaje original-----
> De: [hidden email]
> [mailto:[hidden email]] En nombre
> de Oleg Richards
> Enviado el: Miércoles, 03 de Octubre de 2007 11:03
> Para: [hidden email]
> Asunto: Re: [Seaside] Changing object class
>
> Hmmm.. Thank you guys!
>
> How do you think i can change it:
>   - I have a sheet with different kind of rows
>   - each kind of row is represented by own class
>   - i want to replace one row with another one type
>
> I think that the best way is to delete old one and create
> identically same but as instance of another class. Is this right way?
>
> Cheers, Oleg

What don't you trust your feel about that and take your chance to
implementing what you said? Let the experience to refutate your idea while
you extract all the value you can from that working for a while. That may be
5 minutes 5 weeks or 5 years.

When you found it refutable then you are beign informed by that experience
that obsoletization is necessary to bring a new better one but that moment
will take you when you already have extracted real value from it

Cheers,

Sebastian


_______________________________________________
Seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside