Types

Rook — 1.0 specification

Rook Values In JS
number numbers number
bigint whole numbers of any size, 10n bigint
byte a number from 0 to 255 number
string strings string
bool true, false boolean
any anything, unchecked —
void no value: Promise<void> undefined
never no value at all: a function that never ends —
error an error or null Error or null
T[] an array Array
byte[] bytes Uint8Array
float64[], ... numbers of a fixed size Float64Array, ...
queue T values in order, taken out at both ends $$deque
?T a value of type T or null a value or null
func(A, B) R a function a function
(a A) => R the same function type, written as an arrow a function
comp(a A) a component, which a tag takes from a value a function
A | B a value of type A or of type B a value
"all", 42 exactly that value a value
{ x number } an object with these fields an object
{ [string]: T } an object used as a map: any key, values T an object
a class name an instance of the class an instance
an interface any value with the needed fields and methods —

The type goes after the name, without a colon: x number, xs string[], cb func(number) bool, user ?User.

Typing is structural, as in TypeScript: a value fits a type if it has all the needed fields and methods of the needed types.

bigint

let big = 2n ** 64n           // 18446744073709551616n
let zero bigint               // 0n
const third = big / 3n        // the fraction is dropped
const n = Number(big) + 0.5   // a number again

bigint holds whole numbers of any size, as in JS. A literal ends with n: 10n, 0xffn, 1_000n; it has no fraction or exponent. The zero value is 0n.

byte[]

let header byte[] = [0x89, 0x50, 0x4e, 0x47]
let empty byte[]
const bytes = byte[]("rook")
const text = string(bytes)
let header = new Uint8Array([0x89, 0x50, 0x4e, 0x47])
let empty = new Uint8Array()
const bytes = new TextEncoder().encode('rook')
const text = new TextDecoder().decode(bytes)

byte[] is a Uint8Array, as a byte slice in Go, and byte is a number from 0 to 255 (the range is not checked, as for other numbers). data[i] reads and writes a byte, and the methods are those of Uint8Array: subarray, slice, set, and no push, since its length is fixed.

Typed arrays

const samples = make(float32[], 4)
let weights float64[] = [0.25, 0.5, 0.25]
let ids uint32[]
const counts = make(int64[], 8)
const field = make(float64[], 1_000_000, shared)
const samples = new Float32Array(4)
let weights = new Float64Array([0.25, 0.5, 0.25])
let ids = new Uint32Array()
const counts = new BigInt64Array(8)
const field = new Float64Array($$shared(8 * 1_000_000))

The typed arrays of JS are written as arrays of the number types of Go, as byte[] is: their lengths are fixed, and their elements are numbers of a fixed size, stored in a buffer without boxes.

Rook JS Elements
int8[] Int8Array number, from -128 to 127
uint8[] Uint8Array number, the same as byte
int16[] Int16Array number
uint16[] Uint16Array number
int32[] Int32Array number
uint32[] Uint32Array number
float32[] Float32Array number
float64[] Float64Array number
int64[] BigInt64Array bigint
uint64[] BigUint64Array bigint

Queues

Experimental: may change between releases; see the status at the top.

const jobs = queue<string>(["build", "test"])
jobs.push("deploy")              // at the end
jobs.unshift("lint")             // at the start
const next = jobs.shift()        // ?string, "lint"
const last = jobs.pop()          // ?string, "deploy"
console.log(jobs.length, jobs.at(0), jobs.at(-1))
for job in jobs {
    console.log(job)
}
let waiting queue number         // an empty queue
const jobs = new $$deque(['build', 'test'])
jobs.push('deploy')
jobs.unshift('lint')
const next = jobs.shift()
const last = jobs.pop()
// ...
let waiting = new $$deque()

queue T keeps values of type T in order, and they go in and out at both ends. It has the methods of an array that a queue needs, but shift() and unshift() do not move the other values, as they do in an array, so taking the first of 100 000 values costs as much as taking the first of ten. queue<T>() makes an empty queue, and queue<T>(items) one with the values of an array or of another iterable, which it copies.

Type declarations

type ID string
type Handler func(string) bool
type Pair { first, second number }

type gives a type another name: ID and string are interchangeable (in Go they would be different types). The shape of objects is usually described with interfaces (section 9).

Union and literal types

type Filter "all" | "active" | "done"

func show(value string | number) string {
    if typeof value == "string" {
        return value.toUpperCase()
    }
    return value.toFixed(2)         // here value is already a number
}

Tagged unions

Experimental: may change between releases; see the status at the top.

interface Pickup  { kind "pickup";  shop string }
interface Courier { kind "courier"; address string; floor ?number }
type Delivery Pickup | Courier

interface Order { id number; delivery Delivery }

func where(order Order) string {
    if order.delivery.kind == "courier" {
        return order.delivery.address     // here order.delivery is a Courier
    }
    return order.delivery.shop            // and here a Pickup
}

where({ id: 1, delivery: { kind: "pickup", shop: "Main st." } })

Generics

Type parameters go in angle brackets after the name, as in TypeScript, and a constraint follows its parameter, as in Go:

func first<T any>(xs T[]) ?T {
    return xs.length > 0 ? xs[0] : null
}

interface Box<T> {
    value T
}

type Pair<A, B any> { first A; second B }

class Stack<T> {
    private items T[]
    push(item T) { this.items.push(item) }
    pop() ?T { return this.items.pop() ?? null }
}

const n = first([1, 2, 3])              // ?number
let b Box<string> = { value: "a" }
let s Stack<number> = new Stack()

make

make(T) is the value of a type, which the compiler computes, so that no type is left in the JS:

interface File {
    name string
    size number
    type ?string
}

type MyNumber number

const a = make(File)                    // { name: "", size: 0 }
const b = make(File[])                  // []
const c = make(number)                  // 0
const d = make(MyNumber)                // 0
const a = { name: '', size: 0 }
const b = []
const c = 0
const d = 0

In a generic function, make(T) is the value of the type that each call gives T, and so is the type that decode<T>(x) checks against (section 7). The compiler finds the types of the calls in the module:

func create<T>() T {
    return make(T)
}

const num = create<number>()
const str = create<string>()
function create($$Tmake) {
  return $$Tmake()
}

const num = create(() => 0)
const str = create(() => '')

null and null safety

The language has one "empty" value, null; Rook has no undefined. An ordinary == compiles to ===, but a comparison with null stays loose: x == null → x == null. So it also catches undefined coming from JS code.

null can be assigned only to ?T, error and any. To use a field or a method of a value of type ?T, the type must first be narrowed:

func findUser(id number) ?User { ... }

const u = findUser(1)
console.log(u.name)                  // error: u may be null
console.log(u?.name)                 // fine: the type is ?string
console.log(u?.name ?? "anonymous")  // fine: the type is string

if u != null {
    console.log(u.name)              // here u is a User
}

if u == null {
    return
}
console.log(u.name)                  // and here: on null the function has returned

Markdown source on GitHub