#Extending Native Classes

1 messages · Page 1 of 1 (latest)

charred cape
#

Discussion about creating classes in the core that extend other core classes.

#

@summer umbra if you're still up for it and get a moment sometime I am curious what you learned about extending core classes. I'm wondering if this is hard limitation that is unlikely to ever be possible or if there may be some way it could eventually be made possible.

summer umbra
#

Sure. So summary: it is not possible today as native classes are treated differently from instances of python defined classes. For python defined instances there is logic to look into (and even track) the parent class and find parent class attributes/functions. That does not exist for native classes

#

I believe some work was done for adafruit_pixelbuf to allow a python class to inherit a native base class (which may still be a limitation on micropython)

charred cape
#

I think there are some instances of that for displayio objects as well. Many of our widgets extend either TileGrid or Group which are classes defined in the core.

#

That does make sense though, if there is nothing telling it how to keep track of the fields and functions made available by the superclass. I've always taken somewhat for granted the behavior of inheritance but stopping to a moment to consider how it's actually made possible is an interesting exercise.

summer umbra
#

yeah I discovered native classes are really special compared to ones created in python. A python class instance is type mp_obj_instance_t referring back to the type of the parent

#

Any native class instance is still of the type of the native class defines

#

I think it would be possible to modify the core to support a native subclass of a native class but there is some work to be done. I believe mostly in how new classes are instantiated and how attributes are looked up.

#

For instantiation need to somehow trigger the parent creation. How init and new work in native differs from python too. (Think the calls to super you see). But the native make_new needs to be called and the object type for it has to be instantiated and stored.

And then when attributes are looked up (which is used for SO much) need to both look into parent classes and figure out how when calling an inherited method the data of the parent class is sent. E.g. pixelbuf needs the size parameter which exists in pixelbuf_pixelbuf_obj_t but not in the inherited native subclass type)

charred cape
#

Nice, I'm sure it's over my head at this point. But good to know broadly what direction the effort will need to be in. Something to aspire to learn about to make an attempt at some day.

summer umbra
#

I'm not sure I have it all organized myself but that vaguely is what I found. I do think it is possible to do though.

charred cape
#

Thank you for sharing what you've found from the digging you've done 😄