casting to subclass

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

casting to subclass

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

Re: casting to subclass

Stephan Eggermont-3
Siemen Baader <[hidden email]> 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



Reply | Threaded
Open this post in threaded view
|

Re: casting to subclass

hernanmd
In reply to this post by Siemen Baader
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 <[hidden email]>:

> 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

Reply | Threaded
Open this post in threaded view
|

Re: casting to subclass

Esteban A. Maringolo
In reply to this post by Stephan Eggermont-3
"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 <[hidden email]>:

> Siemen Baader <[hidden email]> 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
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: casting to subclass

Siemen Baader
In reply to this post by Stephan Eggermont-3


On Fri, Mar 2, 2018 at 9:08 PM, Stephan Eggermont <[hidden email]> wrote:
Siemen Baader <[hidden email]> 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




Reply | Threaded
Open this post in threaded view
|

Re: casting to subclass

Siemen Baader
In reply to this post by Esteban A. Maringolo


On Fri, Mar 2, 2018 at 9:21 PM, Esteban A. Maringolo <[hidden email]> 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 <[hidden email]>:
> Siemen Baader <[hidden email]> 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
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: casting to subclass

Siemen Baader
In reply to this post by hernanmd

On Fri, Mar 2, 2018 at 9:19 PM, Hernán Morales Durand <[hidden email]> 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 <[hidden email]>:
> 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