Adding Methods to a Struct
Give your type behavior.
Data Plus Behavior
So far your struct only holds data. A method is a function that lives inside the struct and gives your type behavior. 🚀
Methods Live Inside
You write a method by defining a function indented inside the struct, right alongside its fields and __init__.
struct Point:
var x: Int
var y: Int
fn show(self):
print(self.x, self.y)All lessons in this course
- Defining a struct
- Fields and the __init__ Method
- Adding Methods to a Struct
- Structs vs Python Classes