4. Types and values

4.1 Value types

Taught in 1. Hello, 2. Values, 3. Time, 6. Decisions

Type Literals Notes
text "leader" immutable
number 0, 2.5, -1 one numeric type, 64-bit float
bool true, false
time 150ms, 2s a duration, a whole number of microseconds (0.0005ms is a syntax error); also the type of time.Now (time since the run started)

4.2 Structural types

Taught in 2. Values, 4. First node, 5. Messages, 7. Phrases, 8. Many nodes, 10. Faults, 12. Looks, 13. Story

Type What it is
list of T ordered values of type T, {a, b, c}, indexed from 0
map of K to V keys of type K (text, number, bool or a node type) to values of type V
node any node; identity only (spec/05 §5.1); sim.Nobody is the node value meaning "no node"
Node, p.Node a node running the current package, or package p, with that package's fields
msg any record value (messages and events are records)
<RecordType> a specific record type declared with has, e.g. Prepare, api.Get
shape a drawn shape (look context only)
color a named color (look context only)
field a node field named as a value: Role in its own package, raft.Role elsewhere; only in field holes (sim.Changed)
package an imported package, or the current package by its own name, used as a value: sim.Cluster of 3 running raft
block an indented block passed to a block hole
value any type; only in native patterns (std/), alone or inside list of and map of

A record type name is a valid hole type, and msg accepts any record. Types nest: [ns: list of node], [m: map of node to number], list of list of node. Bare list and map are not types.

4.3 Records

Taught in 5. Messages

A record type is declared with has; each line gives a field and its initial value:

Promise has
  set number to 0
  set prior to 0
  set priorValue to ""

A record value is built with message … with, naming fields (here in node code, where n is the receiver):

message Promise with number n.Promised, prior n.AcceptedBallot, priorValue n.AcceptedValue

Node has uses the same form to declare the fields of the package's node type (see spec/05).

4.4 Lists and maps

Taught in 2. Values, 8. Many nodes

each p in n.peers
  net.Send ping from n to p

each i, p in n.peers
  set item i of n.Last to p

each k in n.Known
  set n.Seen to (lists.Append k to n.Seen)

each k, v in n.Known
  set item k of n.Copy to v

Over a list, p is the item and i the index; over a map, k is the key and v the value. (These four loops are a fragment, not a complete file.)

Unused locals and each names are compile errors (as in Go), so only the form you need compiles. A name is used when it is read or is the base of set item … of x; setting it again is not a use. Names bound by a when pattern and hole parameters may go unused, like Go function parameters. Maps iterate in ascending key order (nodes order by creation), so runs are deterministic.

4.5 Operators

Taught in 2. Values, 6. Decisions

Operator Operands → result
plus, minus number, number → number · time, time → time
times number, number → number · time, number → time · number, time → time
divided by number, number → number · time, number → time · time, time → number
less than, more than, at least, at most number or time pairs → bool
is, is not two values, the type of one accepted as the type of the other (§4.7) → bool
is in, is not in value, list or map → bool
and, or bool, bool → bool
not bool → bool

time times number, number times time and time divided by number round down to a whole microsecond. -0 is 0: no operator yields a negative zero. A non-finite result is a number-range run-time error.

Comparing two values whose types are not accepted as one another (§4.7) is a type-mismatch compile error. Division by zero is a run-time error.

4.6 Type inference

4.7 Accepted types

A value of type S is accepted where type T is expected when one of these holds:

{} is accepted as any list or map type. The join of two types is the one that accepts both: if S is accepted as T it is T; lists and maps join item-wise; otherwise they do not join. is and is not compare two values when the type of one is accepted as the type of the other.