examples
NOTE: These are pulled from the
examples/folder in the repo.
animals
Dog :: struct {
name: string
greeting : string = "woof"
}
Dog :{
greet :: fn(self) string {
"{self.name} says {self.greeting}!"
}
}
dog := Dog.{
name = "Larry"
greeting = "bark"
}
dog.greet()boxes
Box[T] :: struct { v: T }
Box[T] :{
get :: fn(self) T { self.v }
}
b := Box.{ v = 21 }
assert(b.get() == 21)
Box.{ v = "hi" }.get()dimensions
dimensions :: fn() (int, int) {
# TODO: once tuple comparisons ignore labels this can just return with labels
(width = 1920, height = 1080)
}
dimensions()errors
find :: fn(id: int) ?int {
if id == 7 { return 42 }
return none
}
total := 0
loop id in [1, 7, 3, 7] {
total = total + (find(id) or { 0 })
}
totalgrades
Grade :: enum { A B C D F }
grade :: fn(score: int) Grade {
if score >= 90 {
.A
} else if score >= 80 {
.B
} else if score >= 70 {
.C
} else if score >= 60 {
.C
} else {
.F
}
}
grade(89)main
## comments
# Single line comments
# (can be stacked)
#{ Block comments
#{ (can be nested) }#
}#
## Doc comments.
##
## # support markdown
## ```
## # code block language defaults to Oi
## ```
## Main entrypoint.
##
## Called by Oi if present.
main :: fn() {
## primatives
bull := true
str := "string"
integer := 1337
float := 69.420
2.0 + 1.2
}pipelines
("hi" "mom") |> "{$.0}, {$.1}!"points
Point :: struct {
x: int
y: int
}
p := Point.{3, 4}
p.x = p.x + 10
(p.x p.y) |> printranges
total := 0
loop i in 1..10 {
total = total + i
}
assert(total == 45)shapes
Shape :: enum {
point
circle { radius: f64 }
rect { w: float, h: float }
triangle(f64, f64, f64)
}
area :: fn(s: Shape) f64 {
match s {
.circle(r) => 3.14159 * r * r,
.rect(w, h) => w * h,
else => 0.0,
}
}
Shape.rect(w = 3.0, h = 4.0)
|> area
|> assert($ == 12.0)
shape := Shape.triangle(3.0, 4.0, 5.0)
match shape {
.point => {
print("origin: {()}")
}
.circle(r) => {
print("circle: {(r,)}")
}
.rect(w, h) => {
print("rect: {(w h)}")
}
t @ .triangle(a, b, c) => {
print(t)
}
}users
Status :: enum {
offline
online
away
}
User :: struct {
name: string
status: Status
}
badge :: fn(u: User) string {
match u.status {
.online => "🟢 {u.name}",
.away => "🟡 {u.name}",
.offline => "âš« {u.name}",
}
}
User :{
create :: fn(name: string) Self {
# TODO: this should let you create without all fields, zeroing the rest
return User.{ name, status = .offline }
}
greet :: fn(self) string {
match self.status {
.online => "Welcome back, {self.name}!",
.away => "See you soon, {self.name}.",
.offline => "Goodbye, {self.name}.",
}
}
}
user := User.create(
"ur_mom_lolol"
)
# TODO: this should work...
# user.status = .online
user.status = Status.online
# NOTE: string interpolation doesn't work yet
badge(user) |> print
user.greet()