Types

Every expression has a type, known when compiling. There are no implicit conversions, except that a number literal (or a constant made of them) takes the type its place needs: an Int literal can be a Float.

Basic types

typevaluessize
Int32-bit signed integers; arithmetic wraps1 word
Float32-bit IEEE floats1 word
Booltrue and false1 word
Stringa read-only view of bytes2 words
Bloba writable view of bytes2 words

A byte read from a String or Blob is an Int from 0 to 255. Storing an Int as a byte keeps its low 8 bits:

b: Blob[2]
b[0] = 300
b[1] = -1
log(b[0], b[1], "hi"[1])
44 255 105

Arrays, slices, builders

owns its memorya view of it (what procs take)
fixed length[N]T[]T
grows up to a capacity[..N]T (a builder)[..]T (a builder view)
bytesBlob[N], Blob[..N]Blob, Blob[..]

N is a positive Int constant: a number, a constant's name, or arithmetic on those ([W * H]T, [geo.SIZE + 1]T), worked out while compiling. An array indexed by an enum, [E]T, has one element per value of E (see enums). Arrays nest: [4][4]Float is 4 rows of 4.

  • An array [N]T is N elements, one after another. It's a value: assigning it copies it.
  • A slice []T views some elements of an array (2 words: where they start, how many).
  • A builder [..N]T holds up to N elements, and knows how many it has (a length word, then N elements). New builders are empty, unless made from a literal.
  • A builder view [..]T views a builder, or any array, as a builder (3 words: where the length lives, where the elements start, the capacity). Pushing through a builder view changes the length it views.
  • Blob[N] is N bytes, padded to whole words; Blob[..N] is a builder of bytes, for text.
xs: [4]Int               # 4 Ints, all 0
part := xs[1..<3]          # a []Int of 2, viewing xs
part[0] = 7
names: [..8]String       # up to 8; empty for now
_ = names.push("ann")
log(xs[1], part.len(), names.len(), names.cap())
7 2 1 8

Structs

A struct is fields, in the order declared, one after another:

Vec :: struct
  x, y: Float
end
Ship :: struct
  pos, vel: Vec
  hp: Int
  tag: Blob[4]
end
s: Ship
log(s.pos.x, s.hp, s.tag.len())
0.0 0 4

Bitfields

A struct's field can be Int(n), n bits of a word, for n from 1 to 31: unsigned, or signed with Int(-n). It reads as an Int (a signed one's top bit spread over the rest, so Int(-1) holds 0 or -1), and storing keeps the value's low n bits, as storing a byte does. Bitfields fill a word from its low bits, in order; one that doesn't fit in what's left starts a new word (none crosses words), and any other field after them starts a new word too:

Header :: struct
  kind: Int(4)     # bits 0-3
  level: Int(-4)   # bits 4-7
  flags: Int(8)    # bits 8-15
  size: Int(16)    # bits 16-31: the same word
  count: Int       # the next word
end
h := Header{kind = 3, size = 70000}
h.level -= 3
log(h, h.size + 1)
raw: Blob[8]
raw.pack(0, h)
log(raw[0], raw.getInt(2, 16))
Header{kind = 3, level = -3, flags = 0, size = 4464, count = 0} 4465
211 4464

Since a word is little-endian, Int(8), Int(8), Int(16) are its bytes 0, 1 and 2 to 3, so a struct can lay out a binary format byte for byte, for pack and unpack (packing).

A bitfield can also be a Bool(1), or an enum's E(n), n bits with room for every one of E's values, so adding a value an enum's bits can't hold is a compile error, not a broken layout. Each reads, stores and prints as its own type:

Shape :: enum
  circle, box, tri
end
Tile :: struct
  kind: Shape(2)   # bits 0-1: room for 4 values, and Shape has 3
  solid: Bool(1)   # bit 2
  height: Int(5)   # bits 3-7
end
t := Tile{kind = .tri, solid = true, height = 9}
t.kind = .box
raw: Blob[4]
raw.pack(0, t)
log(t, t.kind == .box, raw[0])
Tile{kind = box, solid = true, height = 9} true 77

A bitfield has no address of its own: &h.kind is an error, as is a bitfield type (Int(n), Bool(1), E(n)) anywhere but a struct's field.

Enums

An enum is a named set of values, its own type, stored as an Int. Its values are 0, 1, 2 and so on, in the order listed, so new variables start as the first; listing nil first makes that zero mean "nothing":

Dir :: enum
  north, east, south, west
end
Tile :: enum
  nil
  wall
  water          # 2
end
d: Dir
t: Tile
log(d, t, Tile.water.toInt())
for dir, i in Dir
  log(i, dir)
end
north nil 2
0 north
1 east
2 south
3 west

A value can't be given a number of its own: other numbers, such as a file format's codes, are constants (WALL :: 3), or an array [E]Int of them. [E]T has one element for each value, in order, and for x in E (with an index, for x, i in E) goes through them.

Where an Int goes (assigned to one, passed or returned as one, an element or a field that's one), an enum's value is its number, with no .toInt(). The other way, E.fromInt(n) checks that n is one of its values. Operators still take two of one type (d == 1 and d + 1 are errors), x := d is the enum, an index is an Int (an array indexed by the enum is [E]T), and a []E isn't a []Int:

Way :: enum
  up, right, down, left
end
twice :: proc(n: Int) -> Int
  return n * 2
end
n: Int = Way.down
log(n, twice(Way.left))
2 6

Unions

A union holds a value of one of its types, and remembers which (a tag word, then room for the biggest type). Its zero is the first type's zero, so listing nil first makes an empty union:

Circle :: struct
  r: Float
end
Box :: struct
  w, h: Float
end
Shape :: union
  nil
  Circle, Box
end
s: Shape
log(s == nil)
s = Box{w = 2, h = 3}
log(s is Box)
true
true

Every value of a union takes its biggest type's room, whatever it holds, so a union of a small type and a big one costs the big one's size for each value (in an array of them, for each element), as a big array would: rio -m shows where memory goes.

Small :: struct
  x: Int
end
Big :: struct
  d: [20]Int
end
Shape :: union
  Small, Big
end
s: Shape
log(s is Small, sizeOf(Shape))
true 84

Maybes

?T is a maybe: nil, or a T. It's a union of nil and T (a tag word, then the T), so it starts as nil and works as unions do: a T or nil can be stored in it, x == nil and x != nil test it and narrow it (x == 4 compares its value, false when nil), and a switch takes case nil and case T. It can be written anywhere a type can, and every ?T is the same type, wherever it's written:

Point :: struct
  x, y: Int
end
Enemy :: struct
  hp: Int
  target: ?Point   # (nothing to chase, to start)
end
find :: proc(xs: []Int, want: Int) -> ?Int
  for x, i in xs
    if x == want
      return i
    end
  end
  return nil
end
nums := [3]Int{5, 7, 9}
at := find(nums[], 9)
if at is Int
  log("at", at)  # here, at is the Int
end
log(find(nums[], 4))
e: Enemy
log(e)
e.target = Point{x = 3, y = 4}
log(e.target)
grid: [3]?Int
grid[1] = 5
log(grid)
at 2
nil
Enemy{hp = 0, target = nil}
Point{x = 3, y = 4}
[nil, 5, nil]

Prefixes nest, innermost last: ?[]?Int is a maybe slice of maybe Ints. A ?T can't be used as a T until it's checked:

x: ?Int = 2
log(x + 1)  # error: this may be nil

A union that lists nil already, or a maybe, takes no ?:

y: ??Int  # error: this can already be nil

To tell two kinds of nothing apart, a union can name one with an empty struct, which takes no room:

Missing :: struct
end
Lookup :: union
  nil
  Missing, Int
end
x: Lookup = Missing{}
log(x is Missing, sizeOf(Missing), x)
true 0 Missing{}

A ?T that holds views (?String, ?[]Int) must be a global, as other unions holding them must (see where views can live).

Maybes with if, not, and and or

A maybe works where a Bool does, as if a Bool were a maybe of nothing (true there, false nil):

for Boolsfor maybes
if xwhen truewhen x holds a value, x that value inside (if it's a variable)
not xthe othertrue when x is nil
x and ee if x, else falsee if x holds a value (x that value inside e), else nil
x or ytrue if eitherx's value if it holds one, else y (y runs only then)

x or y, for a ?T x, is a T when y is a T (so it takes no checking after it), and a ?T when y is one too; y can't be of another type, since or joins its two sides into one value. and doesn't join them, so they needn't agree: reading a Bool as a maybe of nothing, x and e is of e's type when e can already be nothing (a Bool, or a maybe), and else a maybe of e's type, nil when x was false or nil:

gives
a and b, for Boolsa Bool
m and m > 5, m a ?Inta Bool
n and n.next, n's next a ?Nodea ?Node (as n?.next would elsewhere)
t and t.size, t's size an Inta ?Int
flag and 3, flag a Boola ?Int

So flag and a or b chooses a or b only when a is neither a Bool nor a maybe (a false or nil a gives b too): an if value says a choice plainly. Only Bools and maybes are conditions: an Int isn't, so if count is still an error:

n := "42".toInt() or 0
m := "7".toInt()
if m
  log("got", m + 1)
end
if not "x".toInt() do log("not a number")
double :: proc(k: ?Int) -> Int
  return k and k * 2 or -1
end
log(n, m and m > 5, double(m), double(nil), true and 3, false and 3)
got 8
not a number
42 true 14 -1 3 nil

A ?Bool isn't a condition, since if x could ask whether it holds a value or whether that value is true: x or false is the second, x is Bool the first.

Procs

A proc type is written proc(T, U) -> R, or without -> R when there's no result (another name for one, in parentheses: Less :: (proc(a, b: Int) -> Bool)). Parameter names are allowed, for reading: proc(x, y: Int) -> Int, and needed for an optional one, proc(x: Int, y?: Int) (see procs). See proc values. Proc values can be elements too, of arrays, slices, builders and [E]T, with the proc type after the brackets (but not a maybe: a proc value is unset until given a proc):

sine :: callback proc(t: Float) -> Float
  return sin(t * 6.2831853)
end
square :: callback proc(t: Float) -> Float
  if t < 0.5
    return 1.0
  end
  return -1.0
end
shapes := [2]proc(Float) -> Float{sine, square}
for shape in shapes
  log(shape(0.25))
end
voices: [..4]proc(Float) -> Float
_ = voices.push(square)
log(voices[0](0.75))
1.0
1.0
-1.0

References

&T is a reference: a name for a place that belongs to someone else. References exist only as parameters and local names, and can't be stored, returned or put in a struct. See references.

Zero values

Every variable starts as zero: 0, 0.0, false, empty strings, slices and builders, an enum's 0 value, a union's first type, and structs and arrays of zeros.

Small and big values

Values up to 16 words (64 bytes) are small: vectors, colors, 4x4 matrices, most structs. Bigger ones can't be copied without saying so, passed by value or returned. See copies.

Where views can live

Views (slices, String, Blob, builder views) can be locals and parameters, and so can structs, arrays and unions that hold them, by the rules for views (see procs). One holding a view of the proc's own memory anywhere in it counts as such a view, as a whole, so it can't be kept past the call either. A field, or an element at a constant index, goes by what was stored there (or in the whole).

Named :: struct
  name: String
  id: Int
end
label :: proc(n: Named) -> String do return n.name
f :: proc()
  typed: Blob[4]
  typed[0] = 'B'
  n := Named{name = typed[0..<1].asString(), id = 2}
  log(label(n), n.id)
end
f()
B 2
Named :: struct
  name: String
  id: Int
end
kept: Named
f :: proc()
  typed: Blob[4]
  kept = Named{name = "ada", id = 1}  # fine: it views a constant
  kept = Named{name = typed.asString(), id = 2}  # error: holds a view of the proc's own memory
end

A builder holding views, or a big value (over 16 words) holding them, must be a global.

A field marked static (name: static String) holds only views that last the whole program, so what's read from it can be kept anywhere: see static views.

Type identity

Structs, enums and unions are the same type only if they're the same declaration. Other types are the same when they're written the same: [4]Int is [4]Int everywhere.