How to copy a subclass from a superclass?
Author |
Message |
Thomas Gagn #1 / 8
|
 How to copy a subclass from a superclass?
Is there 'best' way of doing that? I have a superclass which is persistent that I want to create a subclass from with copies of the superclass' instance variables. The subclass has a single extra instance variable that will be set when its created (copied). Is that what #shallowCopy is used for? But how do I get it to then become the subclass?
|
Mon, 28 Jun 2004 04:17:44 GMT |
|
 |
Thomas Gagn #2 / 8
|
 How to copy a subclass from a superclass?
How about: as: OneOfMySubclasses ^self shallowCopy changeClassTo: OneOfMySubclasses Works in both VW and Gemstone.
|
Mon, 28 Jun 2004 05:25:24 GMT |
|
 |
Eliot Mirand #3 / 8
|
 How to copy a subclass from a superclass?
Quote:
> Is there 'best' way of doing that? > I have a superclass which is persistent that I want to create a subclass from > with copies of the superclass' instance variables. The subclass has a single > extra instance variable that will be set when its created (copied). Is that > what #shallowCopy is used for? But how do I get it to then become the > subclass?
changeClass is the only way to do this without copying the values of the instance variables across. changeClass: can only work if the object has the right number of fields for the class its becoming an instance of. If a class has indexable fields then any object changing class to it must has at least as many fields as the class. So assuming your two classes are not indexable you can't use shallowCopy followed by changeClass because the copy will have one inst var too few. If you do want to use changeClass (because it is indeed quite fast) you'll want to declare the inst var in the superclass but only use it in the subclass. -- _______________,,,^..^,,,____________________________ Eliot Miranda Smalltalk - Scene not herd
|
Mon, 28 Jun 2004 05:52:11 GMT |
|
 |
Thomas Gagn #4 / 8
|
 How to copy a subclass from a superclass?
What seems to be working in both VW and Gemstone is: score: aNumber ^(self shallowCopy changeClassTo: EfiScoredReceipt) score: aNumber EfiScoredReceipt is a transient object. It is a subclass of EfiReceipt with the extra variable, score. I didn't want to change the persistent EfiReceipt object. The alternative would have been to copy each of the instance variables in code from one object to the other. I'm assuming #shallowCopy/#changeClassTo: is a relatively speedy process. Does anyone know that it isn't, or if there's speedier alternatives? -- .tom
|
Mon, 28 Jun 2004 23:33:28 GMT |
|
 |
Terry Raymon #5 / 8
|
 How to copy a subclass from a superclass?
Quote: > What seems to be working in both VW and Gemstone is: > score: aNumber > ^(self shallowCopy changeClassTo: EfiScoredReceipt) > score: aNumber > EfiScoredReceipt is a transient object. It is a subclass of EfiReceipt > with the extra variable, score. I didn't want to change the persistent > EfiReceipt object. > The alternative would have been to copy each of the instance variables > in code from one object to the other. I'm assuming > #shallowCopy/#changeClassTo: is a relatively speedy process. Does > anyone know that it isn't, or if there's speedier alternatives? > -- > .tom
Try the following obj changeClassTo: Array. x := Array new: subclassSize. copy obj into x x changeClassTo: subclass. -- Terry =========================================================== Terry Raymond Smalltalk Professional Debug Package Crafted Smalltalk *Breakpoints* and *Watchpoints* for 80 Lazywood Ln. VW and ENVY/Developer Tiverton, RI 02878
http://www.craftedsmalltalk.com ===========================================================
|
Tue, 29 Jun 2004 02:08:16 GMT |
|
 |
Joseph Bacanska #6 / 8
|
 How to copy a subclass from a superclass?
Hi Thomas: Why not just use a simple wrapper object that holds the persistent EfiReceipt and the non-persistent instVar. Relying so heavily upon inheritance that you must perform serious meta behavior should be telling you that you should revisit some design choices. Just my 2 cents. Quote:
> What seems to be working in both VW and Gemstone is: > score: aNumber > ^(self shallowCopy changeClassTo: EfiScoredReceipt) > score: aNumber > EfiScoredReceipt is a transient object. It is a subclass of EfiReceipt > with > the extra variable, score. I didn't want to change the persistent > EfiReceipt object. > The alternative would have been to copy each of the instance variables in > code from one object to the other. I'm assuming > #shallowCopy/#changeClassTo: is a > relatively speedy process. Does anyone know that it isn't, or if there's > speedier alternatives? > -- > .tom
-- Thanks!! Joseph Bacanskas [|] --- I use Smalltalk. My amp goes to eleven.
|
Tue, 29 Jun 2004 11:47:27 GMT |
|
 |
Thomas Gagn #7 / 8
|
 How to copy a subclass from a superclass?
Well, I had originally done this thing using a wrapper, but was less than impressed with my result, but may still consider it. All of my objects know how to handle printing themselves on a stream in XML. This includes printing their key, complete with their parent's key, data sections, attributes, etc. A wrapper has to implement a bunch of DNU stuff (OK, one DNU thing) so that the object will XMLize itself as expected. In this specific case, I'm creating the subclass so that the copied object can have attributes it's original knows nothing about. In this case, the attribute is an instance variabled called 'score', which is transient and doesn't need to be stored. Is it really wierd to use meta behavior for this kind of thing? In an object oriented system should it be an extraordinary event for one object to project itself (or masquerade) as one of its subclasses? Or should it be odd for a subclass to be created to add transient methods or data from an ancestor? Personally (and I haven't been doing this too long so I may be in left-field) I think it would be unwise to add the variable to the original class and use under-the-hood database stuffs to indicate the variable is never stored (or shouldn't be, at least). Documenting the slight-of-hand would attempt to make obvious something that's is implemented in an unobvious way--whereas the alternative appears suspect because of the unsavory company it keeps--meta class behaviors? As I mentioned before, I had at one time (only a few weeks ago, actually) something called EfiObjectProxy which had a lot of the EfiObject (the parent of almost all my classes) redirected to the wrapped class. But trying to get the wrapper object to intermingle its XML with that of the wrapped object indicated to me (by the amount of effort involved) that there may be a more direct solution. -- .tom
|
Fri, 02 Jul 2004 04:47:54 GMT |
|
 |
Eliot Mirand #8 / 8
|
 How to copy a subclass from a superclass?
Quote:
> Well, I had originally done this thing using a wrapper, but was less than > impressed with my result, but may still consider it. > All of my objects know how to handle printing themselves on a stream in XML. > This includes printing their key, complete with their parent's key, data > sections, attributes, etc. A wrapper has to implement a bunch of DNU stuff > (OK, one DNU thing) so that the object will XMLize itself as expected. In > this specific case, I'm creating the subclass so that the copied object can > have attributes it's original knows nothing about. > In this case, the attribute is an instance variabled called 'score', which is > transient and doesn't need to be stored. > Is it really wierd to use meta behavior for this kind of thing? In an object > oriented system should it be an extraordinary event for one object to project > itself (or masquerade) as one of its subclasses?
No it is not weird. Being able to control serialization behaviour by annotating inst vars is very much in keeping with mechanisms one sees in meta-data systems, persistence systems, distribution systems and else-where (e.g. aspect-oriented systems attempt to subsume these domains). Quote: > Or should it be odd for a > subclass to be created to add transient methods or data from an ancestor?
No; instance-specific behaviour is a rare but old mechanism in Smalltalk. Smalltalk-80 has provided it in various ad-hoc schemes. VSE provided native VM support for this. AOS/SmallScript provides it. Quote: > Personally (and I haven't been doing this too long so I may be in left-field) > I think it would be unwise to add the variable to the original class and use > under-the-hood database stuffs to indicate the variable is never stored (or > shouldn't be, at least). Documenting the slight-of-hand would attempt to make > obvious something that's is implemented in an unobvious way--whereas the > alternative appears suspect because of the unsavory company it keeps--meta > class behaviors?
If performance is important then you'll use a high-performance scheme. Adding the inst var in a superclass would be inexcusable only to the most anally-retentive of people. Documenting the use (i.e. in the class comment) might be very important, though. From an XP perspective do the simplest thing that could possibly work (but no simpler). And from what I can infer, the inst var in the superclass is much simpler. But as soon as it becomes too simple rip it out, do it the simplest way now, and if feeling a little neurotic, cast around for a validating aesthetic. There is no single right way to do anything; there are only trade-offs. Know your constraints and limitations so that "simplest" is evaluated in the appropriate scope; its not just a matter of the code its also its maintainability, migratability, etc, etc. [snip] -- _______________,,,^..^,,,____________________________ Eliot Miranda Smalltalk - Scene not herd
|
Sat, 03 Jul 2004 01:32:01 GMT |
|
|
|