lpython icon indicating copy to clipboard operation
lpython copied to clipboard

Implement inheritance

Open tanay-man opened this issue 1 year ago • 5 comments

Example :

from lpython import i32

class Base:
    def __init__(self:"Base"):
        self.x_A : i32 = 10

class Derived(Base):
    def __init__(self:"Derived") :
        super().__init__()
        self.y_B : i32 = 6
    def get_x_A(self:"Derived"):
        print(self.x_A)
def main():
    d : Derived = Derived()
    print(d.x_A)
    print(d.y_B)
    d.get_x_A()
main()

tanay-man avatar Aug 02 '24 15:08 tanay-man

Can we create a var super and add it to the symtab of the derived class?

super : Base = Base()

So we can search the symtab of the derived and if the member / member fn is not found , we can move to the symtab of the super. Reference - https://docs.python.org/3/library/functions.html#super

tanay-man avatar Aug 02 '24 15:08 tanay-man

@certik, What do you think the best approach here?

Thirumalai-Shaktivel avatar Aug 06 '24 15:08 Thirumalai-Shaktivel

super() in Python is a function. Is the idea to add this function as a method to a class? In Python it's not a method, but a global intrinsic function. Or is the idea to add a super variable? That also seems would differ from CPython.

I would handle super() in the frontend only, and in ASR we need some mechanism to call methods of the parent class. I think you can just call any method directly from the parent symtab, cannot you?

So it seems it's not needed to put super into the symbol table in any form?

certik avatar Aug 07 '24 23:08 certik

Ok got it 👍🏻

tanay-man avatar Aug 08 '24 05:08 tanay-man

Should the members and member functions of the base class be added to the derived class' symtab, or should they be added as an external symbol? @certik @Thirumalai-Shaktivel

tanay-man avatar Aug 10 '24 13:08 tanay-man