Tin

Examples · learn/05-structs · 106 of 339

methods

examples/learn/05-structs/methods.tintin run examples/learn/05-structs/methods.tin
type Rect = { width: float, height: float }

// fn Type.name(...) is a method: the first parameter is the value before the dot.
fn Rect.area(r: Rect): float => r.width * r.height
fn Rect.isSquare(r: Rect): bool => r.width == r.height
fn Rect.scaled(r: Rect, by: float): Rect => Rect { width: r.width * by, height: r.height * by }

fn main() {
  let r = Rect { width: 2.0, height: 3.0 }
  print(r.area())
  print(r.isSquare())
  print(r.scaled(2.0).area())     // methods chain
}