40 lines
1.2 KiB
Mojo
40 lines
1.2 KiB
Mojo
# probe: operator overloading and write_to on a small struct
|
|
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
|
|
# expect-stdout: Vec2(1.5, 0.5)
|
|
# expect-stdout: Vec2(0.5, 3.5)
|
|
# expect-stdout: Vec2(3.0, 6.0)
|
|
# expect-stdout: -2.5
|
|
# expect-stdout: True False
|
|
@fieldwise_init
|
|
struct Vec2(Copyable, Movable, Writable):
|
|
var x: Float64
|
|
var y: Float64
|
|
|
|
def __add__(self, other: Self) -> Self:
|
|
return Vec2(self.x + other.x, self.y + other.y)
|
|
|
|
def __sub__(self, other: Self) -> Self:
|
|
return Vec2(self.x - other.x, self.y - other.y)
|
|
|
|
def __mul__(self, k: Float64) -> Self:
|
|
return Vec2(self.x * k, self.y * k)
|
|
|
|
def __eq__(self, other: Self) -> Bool:
|
|
return self.x == other.x and self.y == other.y
|
|
|
|
def dot(self, other: Self) -> Float64:
|
|
return self.x * other.x + self.y * other.y
|
|
|
|
def write_to(self, mut writer: Some[Writer]):
|
|
writer.write("Vec2(", self.x, ", ", self.y, ")")
|
|
|
|
|
|
def main():
|
|
var a = Vec2(1.0, 2.0)
|
|
var b = Vec2(0.5, -1.5)
|
|
print(a + b)
|
|
print(a - b)
|
|
print(a * 3.0)
|
|
print(a.dot(b))
|
|
print(a == Vec2(1.0, 2.0), a == b)
|