Hi all, it seems like we cannot cast the class of object to its subclass if that subclass has had new instance variables added to it: http://forum.world.st/changing-the-class-of-an-existing-object-to-a-subclass... But I (think) I need it - I'm transforming a DOM tree from XMLHTMLParser's XMLElement s to a subclass of Iliad's ILHTMLBuilderElement s, and for single occurences I would like to use the more specific subclasses, like ILCheckboxElement. I can make the cast manually, I know when it is safe to do. What else could I do, apart from removing the element from its tree, creating a new (subclass) instance and then putting this element in place of the old one? Some #perform or `super` magic..? hm.. thanks for any pointers! -- Siemen
Siemen Baader <siemenbaader@gmail.com> wrote:
What else could I do, apart from removing the element from its tree, creating a new (subclass) instance and then putting this element in place of the old one? Some #perform or `super` magic..?
Use composition? Generate accessing code where necessary. Is this very performance critical code? Stephan
"Castings" are aliens in the Smalltalk world, although there is a #become*: method family, I would discourage you from using it as an alternative unless you really need to do it. If your use case is that concrete, I'd do something like: `ILHTMLBuilderElement fromXMLElement: anXMLElement` Then all subclasses of `ILHTMLBuilderElement` will know what to look and what to omit from the `XMLElement` parameter. Regards! Esteban A. Maringolo 2018-03-02 17:08 GMT-03:00 Stephan Eggermont <stephan@stack.nl>:
Siemen Baader <siemenbaader@gmail.com> wrote:
What else could I do, apart from removing the element from its tree, creating a new (subclass) instance and then putting this element in place of the old one? Some #perform or `super` magic..?
Use composition? Generate accessing code where necessary. Is this very performance critical code?
Stephan
On Fri, Mar 2, 2018 at 9:21 PM, Esteban A. Maringolo <emaringolo@gmail.com> wrote:
"Castings" are aliens in the Smalltalk world, although there is a #become*: method family, I would discourage you from using it as an alternative unless you really need to do it.
Ok, thanks. I think this use case is specific enough for casting to be useful, but I see your point.
If your use case is that concrete, I'd do something like:
`ILHTMLBuilderElement fromXMLElement: anXMLElement`
Yes.. :) -- Siemen
Then all subclasses of `ILHTMLBuilderElement` will know what to look and what to omit from the `XMLElement` parameter.
Regards!
Esteban A. Maringolo
2018-03-02 17:08 GMT-03:00 Stephan Eggermont <stephan@stack.nl>:
Siemen Baader <siemenbaader@gmail.com> wrote:
What else could I do, apart from removing the element from its tree, creating a new (subclass) instance and then putting this element in
place
of the old one? Some #perform or `super` magic..?
Use composition? Generate accessing code where necessary. Is this very performance critical code?
Stephan
On Fri, Mar 2, 2018 at 9:08 PM, Stephan Eggermont <stephan@stack.nl> wrote:
Siemen Baader <siemenbaader@gmail.com> wrote:
What else could I do, apart from removing the element from its tree, creating a new (subclass) instance and then putting this element in place of the old one? Some #perform or `super` magic..?
Use composition? Generate accessing code where necessary.
Hm yes.. This is probably what I'll end up doing eventually. I was trying to save a lot of typing work. XMLElement and ILHTMLBuilder both have useful interfaces, and some of it is called from behind the scenes by their respective frameworks.
Is this very performance critical code?
I don't think so. But it is the HTML construction code of a web app, so it *might* become a bottleneck. I don't treat it like this ATM. Siemen
Stephan
Hi Siemen IIRC this was known as "wide classes", there is some paper about it. Cheers, Hernán 2018-03-02 15:45 GMT-03:00 Siemen Baader <siemenbaader@gmail.com>:
Hi all,
it seems like we cannot cast the class of object to its subclass if that subclass has had new instance variables added to it:
http://forum.world.st/changing-the-class-of-an-existing-object-to-a-subclass...
But I (think) I need it - I'm transforming a DOM tree from XMLHTMLParser's XMLElement s to a subclass of Iliad's ILHTMLBuilderElement s, and for single occurences I would like to use the more specific subclasses, like ILCheckboxElement. I can make the cast manually, I know when it is safe to do.
What else could I do, apart from removing the element from its tree, creating a new (subclass) instance and then putting this element in place of the old one? Some #perform or `super` magic..?
hm..
thanks for any pointers!
-- Siemen
On Fri, Mar 2, 2018 at 9:19 PM, Hernán Morales Durand < hernan.morales@gmail.com> wrote:
Hi Siemen
IIRC this was known as "wide classes", there is some paper about it.
Thanks for the pointer! I found http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.31.4708 but no Pharo implementation. Was it available in Pharo at some point? cheers Siemen Cheers,
Hernán
2018-03-02 15:45 GMT-03:00 Siemen Baader <siemenbaader@gmail.com>:
Hi all,
it seems like we cannot cast the class of object to its subclass if that subclass has had new instance variables added to it:
http://forum.world.st/changing-the-class-of-an-existing- object-to-a-subclass-td4239041.html
But I (think) I need it - I'm transforming a DOM tree from XMLHTMLParser's XMLElement s to a subclass of Iliad's ILHTMLBuilderElement s, and for single occurences I would like to use the more specific subclasses, like ILCheckboxElement. I can make the cast manually, I know when it is safe to do.
What else could I do, apart from removing the element from its tree, creating a new (subclass) instance and then putting this element in place of the old one? Some #perform or `super` magic..?
hm..
thanks for any pointers!
-- Siemen
participants (4)
-
Esteban A. Maringolo -
Hernán Morales Durand -
Siemen Baader -
Stephan Eggermont