Cast instance of superclass to instance of subclass

VA Smalltalk is a "100% VisualAge compatible" IDE that includes the original VisualAge technology and the popular VA Assist and WidgetKit add-ons.

Moderators: Eric Clayberg, wembley, tc, Diane Engles, solveig

Cast instance of superclass to instance of subclass

Postby MartinCz » Wed May 05, 2010 1:09 am

Hi,

I need to cast an instance of a superclass to an instance of a subclass. Is there any way?

I can't change the code that generates the instance of the superclass, so there's no way it could generate an instance of the subclass.

Thank you for any tips.
MartinCz
 
Posts: 7
Joined: Wed Mar 31, 2010 3:56 am

Re: Cast instance of superclass to instance of subclass

Postby TriSebastian » Wed May 05, 2010 2:34 am

Hi!

Well there are different approches available. It depends on the kind of Object you have.

You can either have a look at the class "String" and its implementations like asSymbol and so on.

Another way, that you can find in the RBChangeObjectsApp>>RenameClassChange

convertInstances
| oldClass newClass |
oldClass := self oldClass.
newClass := self newClass.
oldClass allInstances do:
[:each |
| newInst |
newInst := each basicSize > 0
ifTrue: [newClass basicNew: each basicSize]
ifFalse: [newClass basicNew].
1 to: newClass instSize
do: [:i | newInst instVarAt: i put: (each instVarAt: i)].
1 to: each basicSize do: [:i | newInst basicAt: i put: (each basicAt: i)].
each become: newInst]

Hope this helps!

Sebastian
TriSebastian
 
Posts: 76
Joined: Sun Jul 20, 2008 9:40 pm
Location: Nanaimo, BC, Canada

Re: Cast instance of superclass to instance of subclass

Postby MartinCz » Wed May 05, 2010 2:49 am

Hi,

thank you for the prompt reply - in the mean time, I've come to the same conclusion as you - all instance variables need to be copied one by one :) as in Object>>deepCopy or in the method that you found.

Rather than doing this, I decided to choose composition instead of inheritance, so I don't have to deal with this problem :)

Anyway, thank you for your ideas and help.

Have a nice day!
MartinCz
 
Posts: 7
Joined: Wed Mar 31, 2010 3:56 am

Re: Cast instance of superclass to instance of subclass

Postby marten » Wed May 05, 2010 3:02 am

That was a nice question. Here a primitive, that - I would think - could do the work, if both classes have the same structure (same instance variables etc)...

Code: Select all
EsUserPrimitive(mskClassConvert)
{
   EsObject object = NULL;
   EsObject newClass = NULL;

   object = EsPrimArgument(1);
   newClass = EsPrimArgument(2);
   object->class = newClass;
   EsPrimSucceed(object);
}


you would call it via:

Code: Select all
MySuperclass new convertTo: MySubclass


with:

Code: Select all
MySuperclass >>convertTo: aClass

   ^self primConvert: self to: aClass


and:

Code: Select all
MySuperclass >>primConvert: anObject to: aClass

   <primitive: 'mskClassConvert.dll':mskClassConvert>
   ^self primitiveFailed


and you would get an instance of MySubclass ...
Marten Feldtmann, Principal Smalltalk User, Private
SkypeMe callto://marten.feldtmann
marten
[|]
 
Posts: 641
Joined: Sat Oct 14, 2006 7:10 am
Location: Hamburg - Germany

Re: Cast instance of superclass to instance of subclass

Postby MartinCz » Wed May 05, 2010 3:13 am

Wow :shock:

Thank you for this comprehensive answer!! :)
MartinCz
 
Posts: 7
Joined: Wed Mar 31, 2010 3:56 am

Re: Cast instance of superclass to instance of subclass

Postby waynej » Wed May 05, 2010 4:26 am

When I read the question, I did not understand it as needing to convert an instance of one class to that of another class. But it sounds like that is indeed what is desired.
waynej
 
Posts: 32
Joined: Wed Apr 18, 2007 9:18 am


Return to VA Smalltalk 7.0, 7.5 & 8.0

Who is online

Users browsing this forum: No registered users and 1 guest