tomo-koans/lesson-templates/lesson-10-structs.tm
2025-03-25 16:13:54 -04:00

43 lines
885 B
Tcl

# Structs and Methods
# The keyword `struct` is used to define structures
# that hold multiple members:
struct Point(x:Int, y:Int):
# Methods are any function defined inside of the
# indented area below a struct definition.
# There is no implicit `self` argument, only the
# arguments you explicitly define.
func abs(p:Point -> Point):
return Point(p.x:abs(), p.y:abs())
# Constants can be declared inside of a struct's namespace:
ZERO := Point(0, 0)
# Arbitrary functions can also be defined here:
func squared_int(x:Int -> Int):
return x * x
func main():
# You can create a struct instance like this:
p := Point(x=3, y=-4)
>> p
= Point(x=???, y=???)
>> Point.ZERO
= Point(x=???, y=???)
>> p.x
= ???
>> p.y
= ???
>> p:abs()
= ???
>> Point.squared_int(5)
= ???