-
Notifications
You must be signed in to change notification settings - Fork 164
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
Handle class constructor #2730
Comments
Assign: @tanay-man |
Let's do this. from lpython import i32, TypeVar
test = TypeVar("test")
class test:
x: i32 = 0
y: i32 = 0
def __init__(self: test, x: i32, y: i32):
self.x: i32 = x
self.y: i32 = y
# StrucTypeConstrutor(3, 3)
t2: test = test(3, 3) |
Another example: from lpython import i32, TypeVar
test = TypeVar("test")
class test:
kind: str = "abc"
def __init__(self: test, name: str):
self.name = name
t1: test = test("1")
t2: test = test("2")
print(t1.kind)
print(t2.kind)
t2.kind = "d"
print(t1.kind)
print(t2.kind)
print(t1.name)
print(t2.name) |
Instead of:
I recommend doing this:
The first are class variables, the second are instance variables. Let's focus on instance variables first. |
The |
I think we should have just one "test" in the symbol table, which represents the class definition, it will be of type Struct. And the corresponding StructType will be the type of the first argument |
I agree the problem is since TypeVar has the same name, we cannot add Struct (class definition) to the symbol table. |
What does LPython currently put into the symbol table for It seems it should just keep note of this variable internally in AST->ASR, but not expose it in ASR. The |
ASR before adding the
|
I see, the class test:
def __init__(self: "test", x: i32, y: i32):
self.x: i32 = x
self.y: i32 = y |
|
Can we treat |
@tanay-man is suggesting the following example: class test:
def __init__(self, x: i32, y: i32):
... |
I think we can skip the
__init__()
It has to be represented as a StructTypeConstructor, the class_constructor will take care of the rest.
The text was updated successfully, but these errors were encountered: