Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement inheritance #2797

Closed
tanay-man opened this issue Aug 2, 2024 · 5 comments
Closed

Implement inheritance #2797

tanay-man opened this issue Aug 2, 2024 · 5 comments

Comments

@tanay-man
Copy link
Contributor

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
Copy link
Contributor Author

tanay-man commented Aug 2, 2024

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

@Thirumalai-Shaktivel
Copy link
Collaborator

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

@certik
Copy link
Contributor

certik commented Aug 7, 2024

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?

@tanay-man
Copy link
Contributor Author

Ok got it 👍🏻

@tanay-man
Copy link
Contributor Author

tanay-man commented Aug 10, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants