Declarations

:: declares something that doesn't change: a constant, a type, a proc, or another name for one. := and : declare variables.

Before use

Every name must be declared before it's used, in the order the source is read. So a proc can't call itself, or a proc declared after it, and Rio has no recursion:

f :: proc()
  g()  # error: undefined name
end
g :: proc()
end

Constants

NAME :: value names a value known when compiling: a number, Bool or String, or operators and conversions on those. A constant number takes the type its place needs, as a literal does:

N :: 16
HALF :: N / 2
SCALE :: 1.5
NAME :: "rio"
x: Float = N             # 16.0: N is a number, not just an Int
log(HALF, x, NAME)
8 16.0 rio

Constants can be declared in procs too. Top-level constants can be set from outside: see defines.

Variables

count := 0              # the type from the value: Int
speed: Float = 2        # the type written, and a value
grid: [4][4]Int         # the type written: starts as zero
log(count, speed, grid[3][3])
0 2.0 0

A variable at the top level is a global, living as long as the program. One in a proc is a local; it starts again each time its declaration runs. A declaration is visible from there to the end of its block. A name is declared once where it's seen:

  • At a module's top level (its parts included), a second variable, constant, proc or type of the same name is an error, which keeps a part's globals from quietly clashing with another's.
  • A local (a parameter too) can't hide another local still in scope: declaring it again in the same block or an inner one is an error, so x := 5 written for x = 5 can't quietly make a new x. A block that's ended frees its names: two loops one after another can each have an i.
  • A local may hide a global or a builtin, which a proc's author needn't know all of:
x := 1
f :: proc()
  x := 2.5               # a local x, in f only
  for i in 0..<2
    x += 1.0
  end
  for i in 0..<1         # (the first i's loop has ended)
    log(x)
  end
end
f()
log(x)
4.5
1

The REPL is the exception: each line typed may declare a name again, as trying things out needs.

Copies

Assigning, passing and returning copy values. To keep copies cheap and visible, a copy that isn't written out is limited to 16 words: a bigger x := big is an error. Write the type to copy on purpose, or take a view (big[]) or a reference (&big). Parameters and results are limited to 16 words; pass bigger values as &T or []T, and fill in a &T parameter instead of returning one. Plain assignment, b = a, copies any size.

Big :: struct
  a: [20]Int
end
g: Big
f :: proc()
  copy: Big = g          # fine: the type says it's a copy
  x := g  # error: this copies a big struct
end

Procs

add :: proc(a, b: Int) -> Int
  return a + b
end
greet :: proc(name: String)
  log("hi", name)
end
greet("ann")
log(add(2, 3))
hi ann
5

A parameter marked with ?, as in scale?: Int, is optional: a call may leave it out, and then it's zero, as a variable is (nil for a maybe, empty for a String, a struct's fields all zero). Only the last parameters can be optional, so arguments still go by position, and forgetting any other one is still an error. A struct last makes a proc's options easy to leave out or give a few of:

Style :: struct
  scale: Int
  shadow: Bool
end
label :: proc(text: String, x: Int, style?: Style)
  log(text, x, style.scale, style.shadow)
end
label("hi", 4)
label("hi", 4, {scale = 2})
hi 4 0 false
hi 4 2 false
f :: proc(a?: Int, b: Int)  # error: after an optional parameter (name?: T), the rest are optional too
end

A & parameter can't be optional, since it needs a place. A proc type says which parameters are optional the same way, proc(a: Int, b?: Int), and it's part of the type: a proc goes where a proc value does only if the two agree.

Procs are declared at the top level. Parameters are values the proc can change without affecting the caller, except &T parameters (see references). A proc with a result must end with return on every path:

sign :: proc(n: Int) -> Int
  if n < 0
    return -1
  end
end  # error: missing return

A proc can't return a view of its own locals, or store one where it outlives the call (a global, or through a & parameter): the next call reuses that memory.

f :: proc() -> []Int
  xs: [3]Int
  return xs[]  # error: this returns a view of the proc's own memory
end

The same goes for a struct, array or union holding views, such as a struct with a String field or a ?String: it can be returned if what it holds views memory other than the proc's own.

Entry :: struct
  key: String
  n: Int
end
entry :: proc(line: String) -> Entry
  for c, i in line
    if c == '=' do return Entry{key = line[0..<i], n = line[i + 1..].toInt() or 0}
  end
  return Entry{key = line}
end
firstWord :: proc(s: String) -> ?String
  for c, i in s
    if c == ' ' do return s[0..<i]
  end
  return nil
end
e := entry("hp=12")
log(e.key, e.n, firstWord("hello there") or "-", firstWord("one") or "-")
hp 12 hello -

A view that comes in as a parameter may be one of the caller's own, so it's only borrowed: it can't be stored where it outlives the call either. To keep what it views, copy the bytes:

name := ""
rename :: proc(s: String)
  name = s  # error: this view came in as a parameter
end
name: Blob[..32]
rename :: proc(s: String)
  name.clear()
  ok := name.pushAll(s)
end
f :: proc()
  typed: Blob[8]
  typed[0] = 'A'
  rename(typed[0..<1].asString())
end
f()
log(name.asString())
A

What's read through a & parameter or self is the caller's too, so a view read there is only borrowed in the same way. A call's result, when it's a view or holds one, may view what went in, so it counts as a view of whatever those were. That includes the places of & arguments and self, when there's an array or builder in them for a view to view:

Box :: struct
  xs: [4]Int
end
front :: proc(b: &Box) -> []Int
  return b.xs[0..<2]
end
kept: []Int
f :: proc()
  b: Box
  kept = front(&b)  # error: this view of the proc's own memory would outlive the call
end

Which locals hold such views is worked out in the order the code is written. A store in a local's own block (the one it's declared in) replaces what it held. A store in an if or a loop inside that block only adds to it, since it may not run. And in a loop, the code before a store runs after it too, so a local from before the loop can't start holding such a view there:

kept: []Int
g: [4]Int
f :: proc(c: Bool)
  xs: [4]Int
  v := xs[]
  v = g[]  # its own block: v views only g now
  kept = v
  w := xs[]
  if c
    w = g[]
  end
  kept = w  # error: this view of the proc's own memory would outlive the call
end
kept: []Int
g: [4]Int
f :: proc()
  xs: [4]Int
  v := g[]
  for _ in 0..<2
    kept = v
    v = xs[]  # error: in a loop, into a variable from before it that held none
  end
end

And a call's result, when it's a view or holds one, may view what went in, so it's a view of the caller's own memory if any argument was. Each rule is about one proc's own code, so changing a proc's body never changes what its callers may do.

Static views

A view that lasts the whole program (a literal's, a constant's or a global's) can be kept anywhere. Marked static, a signature or a struct says that's all it takes, or all it gives, and then it may keep what it's given. Each use is checked against the claim, never against what a body happens to do:

  • A parameter, s: static String: each call must pass a view that lasts, and the proc may keep it. A static parameter passed on lasts too.
  • A result, -> static String: the proc returns only views that last, so a caller may keep the result whatever it passed in.
  • A field, name: static String: only views that last are stored there, so what's read from it lasts, even from a local or a borrowed struct.
  • A method's self, self: static first in its parameters: the method may keep views of self (its arrays, say) or of what self holds, so it's called only on a global.
said := ""
say :: proc(s: static String)
  said = s
end
Thing :: struct
  name: static String
  hp: Int
end
title :: proc(t: Thing) -> static String
  if t.hp > 0 do return t.name
  return "gone"
end
f :: proc()
  t := Thing{name = "lamp", hp = 3}
  say(title(t))
end
f()
log(said)
Menu :: struct
  items: [3]String
  shown: []String
end
Menu.showAll :: proc(self: static)
  self.shown = self.items[]
end
menu := Menu{items = {"tea", "toast", "jam"}}
menu.showAll()
log(menu.shown.len(), menu.shown[1])
lamp
3 toast
said := ""
say :: proc(s: static String)
  said = s
end
f :: proc(name: String)
  say("hi")  # fine: a literal lasts
  say(name)  # error: a static parameter
end
Thing :: struct
  name: static String
end
f :: proc()
  typed: Blob[4]
  typed[0] = 'A'
  t := Thing{name = typed[0..<1].asString()}  # error: this field is static
end
Menu :: struct
  items: [3]String
  shown: []String
end
Menu.showAll :: proc(self: static)
  self.shown = self.items[]
end
f :: proc()
  m: Menu
  m.showAll()  # error: self: static
end

static is about how long what's viewed lasts, not about changing it: a static field can be set again, to another view that lasts. It goes only on a view (a String, a Blob, a []T) or a value holding views, and not on a & parameter. A proc with a static parameter or self can't be a proc value yet, since a call through a proc type wouldn't check what's passed. static is a word only in these places (and on static procs), so it reserves nothing.

Methods

Type.name :: proc declares a method on a type. Inside, self is the value it was called on: for structs, the caller's own value; for Int, Float, Bool, String and Blob, a copy.

Ship :: struct
  x, vx: Float
  hp: Int
end
Ship.move :: proc(dt: Float)
  self.x += self.vx * dt
end
Ship.hurt :: proc(n: Int) -> Bool
  self.hp -= n
  return self.hp <= 0
end
Int.double :: proc() -> Int
  return self * 2
end
s := Ship{vx = 2, hp = 3}
s.move(0.5)
log(s.x, s.hurt(5), 21.double())
1.0 true 42

Each type has its own method names, apart from plain procs. A method can't have the same name as a field of its type. Fields are always self.x; a plain x is never a field.

Type.name :: static proc declares a static proc: one called on the type, Type.name(...), with no self, as a constructor is (static is a word only there, and in static views). It's one of the type's method names, and travels with the type as its methods do, but a value can't call it, and the type can't call a method:

Rect :: struct
  pos, size: [2]Float
end
Rect.of :: static callback proc(x, y, w, h: Float) -> Rect
  return Rect{pos = {x, y}, size = {w, h}}
end
Rect.area :: proc() -> Float
  return self.size.x * self.size.y
end
r := Rect.of(1, 2, 3, 4)
make := Rect.of  # a proc value, as any proc's name is
log(r.area(), make(0, 0, 2, 2).area())
12.0 4.0

Structs, enums and unions

Vec :: struct
  x, y: Float            # several fields of one type
end
Dir :: enum
  north, east, south, west
end
Shape :: union
  Circle, Box
end

Field types, and the types in a union, must already be declared, so no type contains itself. See types for what they mean. Types are declared at the top level.

Other names for things

:: with an existing type, proc or module makes another name for it:

add :: proc(a, b: Int) -> Int
  return a + b
end
plus :: add
Point :: [2]Float
p: Point
log(plus(1, 2), p.len())
3 2

A proc type takes parentheses, since Less :: proc(a, b: Int) -> Bool alone would start a proc of that name:

Less :: (proc(a, b: Int) -> Bool)
lt :: callback proc(a, b: Int) -> Bool
  return a < b
end
sorted :: proc(xs: []Int, less: Less) -> Bool
  for i in 1..<xs.len()
    if less(xs[i], xs[i - 1]) do return false
  end
  return true
end
xs := [3]Int{1, 2, 3}
log(sorted(xs[], lt))
true

Exports

A * after a top-level name exports it: other modules can use it, and the host can find it (see modules and programs):

Vec* :: struct
  x, y: Float
end
score* := 0
update* :: proc()
end

Generic procs

proc[T, U](...) names types a proc works on whatever they are. Each is found at every call, from the arguments, so each must be in a parameter's type. The proc is compiled once, for every T: a call passes, unseen, what T is, and so its size. In the body a T's size is known only while running, so a T is only ever reached through a reference or a slice, never held:

  • Where a T goes: x: &T, xs: []T and xs: [..]T, as parameters, in a proc type's parameters (less: proc(a, b: &T) -> Bool), and in a variable as []T or [..]T.
  • What a T does: it's copied from place to place (xs[i] = x, a = b), swapped (xs.swap(i, j)), and passed on, to a generic proc or a proc value as &xs[i]. A []T is indexed, sliced and walked with for x in xs (x is the element itself, as for any slice). A [..]T has its builder methods too: xs.push(x) copies from a place, and xs.pop() gives the last one's place to copy from (out[i] = xs.pop(), ys.push(xs.pop())). Anything else is an error: comparing (pass in a proc that compares), arithmetic, printing, keeping one in a variable. A generic proc can't return a T, or be a proc value.
  • What a call checks: each T found agrees everywhere it's found. And a proc that copies its T into memory it was given can't take a T holding views (a String, a struct with one): it could keep a view past what it views, as any proc could by storing a borrowed view (xs.swap is fine, since both places are in xs).
sortBy :: proc[T](xs: []T, less: proc(a, b: &T) -> Bool)
  for i in 1..<xs.len()
    j := i
    for j > 0 and less(&xs[j], &xs[j - 1])
      xs.swap(j, j - 1)
      j -= 1
    end
  end
end
find :: proc[T](xs: []T, x: &T, eq: proc(a, b: &T) -> Bool) -> Int
  for i in 0..<xs.len()
    if eq(&xs[i], &x) do return i
  end
  return -1
end
Player :: struct
  name: String
  score: Int
end
byScore :: callback proc(a, b: &Player) -> Bool do return a.score < b.score
intEq :: callback proc(a, b: &Int) -> Bool do return a == b
players := [_]{Player{name = "ann", score = 30}, Player{name = "bo", score = 10}}
sortBy(players[], byScore)
nums := [_]{4, 8, 15}
eight := 8
nine := 9
log(players[0].name, find(nums[], &eight, intEq), find(nums[], &nine, intEq))
bo 1 -1

A proc type can take types too, named and then filled in where it's used: Less :: (proc[T](a, b: &T) -> Bool), then Less[Point], or Less[T] in a generic proc. It's the same type as one written out, so sortBy :: proc[T](xs: []T, less: Less[T]) reads as the one above.

A struct can take types the same way, Table :: struct[K, V], filled in where it's named: Table[Int, Point], or Table[K, V] in a generic proc, which finds K and V through it. A T goes in a field only in a []T or a proc type, so every field's size is the same whatever T is: each filled-in struct is its own type, laid out as the others are, and no code is made per type. Each type parameter must be in some field's type. A literal can leave the types out, Table{keys = ks[], ...}: each field's value finds the ones its type has, in the order they're written, and every one must be found by the end. A {...} going into a field whose type isn't known yet is an error: name the types then.

A generic struct's methods name its types first, in order, as a generic proc does: Table.put :: proc[K, V](key: &K, value: &V), with self a Table[K, V], found from the value it's called on. More can follow (proc[K, V, U]), found from the arguments. A static proc on one is generic the same way, found from its arguments, Table.make(ks[], vs[], eq), or named, Table[Int, Point].make(...). Any type's methods can be generic too: Int.pick :: proc[T](xs: []T) -> T.

Table :: struct[K, V]
  keys: []K
  values: []V
  eq: proc(a, b: &K) -> Bool
end
find :: proc[K, V](t: &Table[K, V], key: &K) -> Int
  for i in 0..<t.keys.len()
    if t.eq(&t.keys[i], &key) do return i
  end
  return -1
end
intEq :: callback proc(a, b: &Int) -> Bool do return a == b
ks := [_]{7, 3}
names := [_]{"seven", "three"}
t := Table{keys = ks[], values = names[], eq = intEq}  # a Table[Int, String]
three := 3
log(t.values[find(&t, &three)], t)
three {keys = [7, 3], values = ["seven", "three"], eq = proc}

The ideas behind them

Generics follow from the rest of Rio, and these are worth checking new code against:

  1. Storage gives places; computation gives values. for x in xs, a &T parameter, self, xs[i] and p.f are places. A call's result, a next() result and xs.pop() are computed: they're copied, not borrowed (&xs.pop() is an error, since the next push reuses its slot). A T is always a place, since a generic body can't give one a home of its own.
  2. Copies are visible. a = b copies, so does xs.push(x), and every T copy is from place to place.
  3. A proc stores only what it owns. A view or T that came in borrowed can't be stored where it outlives the call. A generic proc that copies its T into memory it was given takes no T holding views; moving within one sequence (swap, remove, swapRemove) is fine.
  4. What depends on the type comes in as a proc: comparing, ordering, hashing (less, eq, hash). There are no constraints on a T.
  5. Values decide types, in the order written: [_]{...}, a generic call's arguments, and a generic struct's fields, Table{...}. The first that settles a type wins, and the rest must agree.
  6. Type parameters go on the keyword and are filled in by name: proc[T], struct[K, V], Name[...]. A call never names them, so [...] after a value is always indexing.

A generic container gives its elements as slices to walk, not through next(), which would return a T by value.

Host procs

host proc and host struct declare something the host provides: see host procs. Elsewhere, host is an ordinary name.