lpython icon indicating copy to clipboard operation
lpython copied to clipboard

Add support for class members

Open xaerru opened this issue 11 months ago • 2 comments

Fix #2723

Add support for class members by identifying them by the new intent ClassMember and creating a global variable for each class member.

All the tests are not passing yet, but I need a review to know if I'm going in the right direction.

xaerru avatar Jan 24 '25 19:01 xaerru

The tests are now passing.

I now need to implement functionality for when an instance variable accesses a class member. If it performs a read, it should access the global variable. But, if it writes to the class member, a copy should be made for that specific instance and used for any further reads and writes.

Currently, instance variables only access the global class member.

class A:
    c: i32 = 10
    def __init__(self:"A") -> None:
        self.d: i32 = 20

a: A = A()
a.c = 30 # Makes a copy
print(a.c) # 30
print(A.c) # Should be 10, it is currently 30

I wanted advice on how to implement this and also if the current design of using global variables for class members is good or not.

One way can be adding the class members definitions to __init__'s body. But then we need to keep track if a copy has been created or not.

xaerru avatar Jan 30 '25 13:01 xaerru

@xaerru they fixed CI's it should pass if u rebase

swamishiju avatar Mar 21 '25 06:03 swamishiju