lpython
lpython copied to clipboard
Polymorphic function args dont work
from lpython import i32
class Base:
def __init__(self:"Base"):
self.x_A : i32 = 10
class Derived(Base):
pass
def check(x: Base):
print(x.x_A)
def main():
d : Derived = Derived()
check(d)
main()
Op:
(base) tanay@tanay-man:~/lcompilers/lpython$ lpython integration_tests/class_05.py
semantic error: Type mismatch in procedure call; the types must be compatible
--> integration_tests/class_05.py:21:11
|
21 | check(d)
| ^ type mismatch (passed argument type is struct Derived but required type is struct Base)
|
15 | def check(x: Base):
| ^^^^ type mismatch (passed argument type is struct Derived but required type is struct Base)
I think the subclass has to be automatically cast to the base class in this case.
Let's represent the Cast node in the ASR
def main():
d : Derived = Derived()
check(d)
will become:
def main():
d : Derived = Derived()
check(Cast(d, DerivedToBase, Base, nullptr))
Using the Base name, get the Base type in the asr to llvm backend.
@Thirumalai-Shaktivel I have implemented the ASR level cast node for this. How to get the cast working in the llvm backend? Current op-
(lp) tanay@tanay-man:~/lcompilers/lpython$ lpython integration_tests/class_06.py
code generation error: Cast kind not implemented