code / tomo-koans

Lines447 Tomo432 INI9 Markdown6
(36 lines)
1 # Structs and Methods
3 # The keyword `struct` is used to define structures
4 # that hold multiple members:
5 struct Point(x:Int, y:Int)
7 # Methods are any function defined inside of the
8 # indented area below a struct definition.
10 # There is no implicit `self` argument, only the
11 # arguments you explicitly define.
12 func abs(p:Point -> Point)
13 return Point(p.x.abs(), p.y.abs())
15 # Constants can be declared inside of a struct's namespace:
16 ZERO := Point(0, 0)
18 # Arbitrary functions can also be defined here:
19 func squared_int(x:Int -> Int)
20 return x * x
22 func main()
24 # You can create a struct instance like this:
25 p := Point(x=3, y=-4)
27 assert p == Point(x=???, y=???)
29 assert Point.ZERO == Point(x=???, y=???)
31 assert p.x == ???
32 assert p.y == ???
34 assert p.abs() == ???
36 assert Point.squared_int(5) == ???