36 lines
890 B
Mojo
36 lines
890 B
Mojo
# probe: a trait with a default method, used by two structs
|
|
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
|
|
# expect-stdout: a shape of area 7.0
|
|
# expect-stdout: a square of side 4.0
|
|
trait Shape:
|
|
def area(self) -> Float64:
|
|
...
|
|
|
|
def describe(self) -> String:
|
|
return "a shape of area " + String(self.area())
|
|
|
|
|
|
@fieldwise_init
|
|
struct Rect(Shape, Copyable, Movable):
|
|
var w: Float64
|
|
var h: Float64
|
|
|
|
def area(self) -> Float64:
|
|
return self.w * self.h
|
|
|
|
|
|
@fieldwise_init
|
|
struct Square(Shape, Copyable, Movable):
|
|
var side: Float64
|
|
|
|
def area(self) -> Float64:
|
|
return self.side * self.side
|
|
|
|
def describe(self) -> String:
|
|
return "a square of side " + String(self.side)
|
|
|
|
|
|
def main():
|
|
print(Rect(2.0, 3.5).describe())
|
|
print(Square(4.0).describe())
|