turbo-editors/turbo-golopublic Fork 0
v1.0.0
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-golo.git
git clone ssh://git@rickub.com/turbo-editors/turbo-golo.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

📦 Turbo Golo d710c1b · on v1.0.0 · k33g · 15h ago
shapes.golo · 41 lines · 1.1 KBMySQL Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module demo.Shapes

# A struct is a named record. Its fields are read with a colon: p: x().
struct Point = { x, y }

# A union is one type with several forms. Each variant may carry fields, and
# every variant gets an is<Variant>() predicate for free.
union Shape = {
  Circle = { radius }
  Rect = { width, height }
}

# An augmentation adds functions to a type after the fact. `this` is the value
# the function is called on.
augment Point {
  function describe = |this| -> "(" + this: x() + ", " + this: y() + ")"
}

augment Shape {
  function area = |this| {
    return match {
      when this: isCircle() then 3.14159 * this: radius() * this: radius()
      when this: isRect() then this: width() * this: height()
      otherwise 0
    }
  }
}

function main = |args| {
  let origin = Point(0, 0)
  println("origin is " + origin: describe())

  let shapes = list[Shape_Circle(1.0), Shape_Rect(2.0, 3.0)]
  foreach shape in shapes {
    println(shape + " has area " + shape: area())
  }

  # A closure is a function value: |parameters| -> expression.
  let scaled = list[shape: area() * 2 foreach shape in shapes]
  println("doubled: " + scaled)
}