Open ykutaykuta opened 1 year ago
Current behavior is align with python. You maybe get mistake.
Current behavior is align with python. You maybe get mistake.
In Python we can do
class Complex:
real: float
imag: float
def __init__(self, real, imag):
Complex.real = real
Complex.imag = imag
def dump(self) :
print(str(Complex.real) + " + " + str(Complex.imag) + "i")
if __name__ == "__main__":
a = Complex(1.2, 2.3)
b = Complex(4.5, 5.6)
a.dump()
b.dump()
How to do the same in Mojo, I just try it but Mojo error: cannot access instance field 'real' without an instance of 'Complex'
We currently support static methods, but not static data yet. This is tied into global variable support.
Review Mojo's priorities
What is your request?
struct Complex: var real: Float32 var imag: Float32
fn main() raises: var a = Complex(1.2, 2.3) var b = Complex(4.5, 5.6) a.dump() b.dump()
Output: 1.2000000476837158 + 2.2999999523162842i 4.5 + 5.5999999046325684i
Expected output: 4.5 + 5.5999999046325684i 4.5 + 5.5999999046325684i
What is your motivation for this change?
Static method, and static attribute is useful in many case. It's a part of programing
Any other details?
No response