The Zeno Book Zeno 0.1 zeno-lang.org

16 Types, Written and Worked Out

Every value in Zeno has a type: 3 is an int, "hi" is a str, [1, 2] is a list[int]. Types can be written in a program or left out. This chapter says what happens in each case, what Zeno works out by itself, what it does when it cannot, and the strict mode that a compiled program needs.

16.1 The types there are

Type Holds
int a whole number (Numbers and Operators)
float a number with a point
bool true or false
str text (Text)
list[T] items of type T, in order (Lists)
dict[K, V] values of type V under keys of type K (Dicts)
function(A, B) -> R a function (Functions as Values)
T or none a T, or nothing (Values That May Be Missing)
a class or enum name an object or a case (Classes, Enums)
an interface name any class that is that interface (Interfaces)
any a value of any type, checked as the program runs

Later chapters add numbers with units such as ms and V (Numbers with Units), sized whole numbers such as u8 (Sized Numbers and Bits), and the pins and registers of a board (The Board).

16.2 Writing a type

A type goes after a name, with a colon, on a variable, a parameter or a field, and after -> for what a function gives back:

let limit: int = 100
var names: list[str] = []
function scale(value: float, by: float) -> float {
    return value * by
}
class Reading {
    pin: int
    volts: float
}
print(limit, names, scale(2.0, 1.5), Reading(4, 3.3))
100 [] 3.0 Reading(pin=4, volts=3.3)

A written type is a promise. Zeno checks every use against it before the program runs, and never changes it to fit:

function scale(value: float, by: float) -> float {
    return value * by
}
print(scale(2, 3))
error: `value` of `scale` should be float, but this is int. Write a number with a decimal point, as in `1.0`, and convert a value with `float( )`
 --> main.zeno:4:13
  |
4 | print(scale(2, 3))
  |             ^

error: `by` of `scale` should be float, but this is int. Write a number with a decimal point, as in `1.0`, and convert a value with `float( )`
 --> main.zeno:4:16
  |
4 | print(scale(2, 3))
  |                ^

2 errors found. Nothing was run.

The name is what a reader scans for, so it comes first, and since the type can usually be left out, the part that may be missing goes last. (Decision 1.11.)

16.3 What Zeno works out

Where a type is left out, Zeno works it out from the program, wherever it can be known for certain.

A variable

has the type of the value it starts with. let n = 3 is an int, and stays one:

var count = 3
count = "three"
error: the new value should be int, but this is str
 --> main.zeno:2:9
  |
2 | count = "three"
  |         ^

1 error found. Nothing was run.

What a function gives back

is worked out from its returns, in recursive functions too.

A parameter

is worked out from the calls. If every call passes ints, the parameter is an int, and the body is checked with that knowledge:

function add(a, b) {
    return a + b
}
let total: str = add(1, 2)
error: `total` should be str, but this is int
 --> main.zeno:4:18
  |
4 | let total: str = add(1, 2)
  |                  ^

1 error found. Nothing was run.

A field

is worked out from every value stored in it:

class Reading {
    pin
    volts
}
let r = Reading(3, 2.5)
let label: str = r.volts
error: `label` should be str, but this is float
 --> main.zeno:6:18
  |
6 | let label: str = r.volts
  |                  ^

1 error found. Nothing was run.

Types pay for themselves in large programs and in firmware, where a mistake found on the bench costs hours. They get in the way of a ten-line script. With types that can be left out, one language serves both, and a program can start loose and be tightened a line at a time as it grows, instead of being written again in another language. This is called gradual typing; TypeScript and Python’s type hints took the same road. (Decision 0.2.)

Where the mistake is reported

When a parameter’s type was worked out, a call that disagrees is reported inside the function, where the value is misused, with a note that names the call the type came from:

function shout(text) {
    return text.upper()
}
print(shout(5))
error: int has no field or method `upper`
 --> main.zeno:2:12
  |
2 |     return text.upper()
  |            ^
  = note: `text` was worked out to be int from line 4. Give `text` a type to have each call checked against it instead

1 error found. Nothing was run.

Zeno cannot know which line is wrong, the function or the call, so it shows both. Writing the type settles it, and then the call is the mistake. That is the reason to write types on anything other people call.

16.4 When it cannot be worked out

Some things have no one type. A function called once with an int and once with text, or a list that holds a number and a word:

function same(x) {
    return x
}
let mixed = ["volts", 3.3, true]
print(same(1), same("one"), mixed)
1 one ["volts", 3.3, true]

Such a value is dynamic: it carries its type with it, and is checked while the program runs instead of before. Zeno never guesses a type; when two uses disagree, it leaves the value dynamic. The type any says so on purpose:

let a: any = 3
print(a + 1)
let b: any = "three"
print(b + 1)
4
error: `+` does not work between a str and an int
 --> main.zeno:4:7
  |
4 | print(b + 1)
  |       ^

Here the first print runs, and the mistake in the second is found only when the program reaches it. That is the price of a dynamic value: a mistake that a type would have caught before the program started is caught later, or on a path that no test happened to take, never.

16.5 Strict mode

A dynamic value needs a tag that says what it is, and usually memory from the heap. A small microcontroller cannot afford either, so a compiled program must know every type before it runs. Strict mode is that rule: a value that would be dynamic is an error.

Strict mode is asked for with --strict on the command line, or by the word strict alone on the first line of the file. zeno build always works in strict mode.

function same(x) {
    return x
}
print(same(1), same("one"))
$ zeno check --strict main.zeno
error: strict mode: the parameter `x` of `same` needs a type. It could not be worked out from how it is used
 --> main.zeno:1:1
  |
1 | function same(x) {
  | ^

1 error found. Nothing was run.
strict
let mixed: list[any] = ["volts", 3.3]
error: `any` is not allowed in strict mode
 --> main.zeno:2:1
  |
2 | let mixed: list[any] = ["volts", 3.3]
  | ^

1 error found. Nothing was run.

Most programs pass strict mode with few types written, because Zeno works out so much. A program written with no types at all, whose functions are each called with one kind of value, is strict already.

Whether a file may hold dynamic values is a fact about that file: firmware must not, a script may. A flag on the command line lives in a build script, where it can be forgotten; a first line travels with the code and is seen by everyone who reads it. It is a bare word, with no # or @, because it is part of the language and not a comment. There is no way to turn strict mode off from inside a file: the stricter of the two always wins. (Decision 0.2.)

16.6 No quiet conversions

Zeno does not change a value from one type to another behind the program’s back. An int is not a float, though the two mix in arithmetic (Numbers and Operators), and nothing becomes a bool or a str by itself:

let whole = 3
let real: float = whole
error: `real` should be float, but this is int. Write a number with a decimal point, as in `1.0`, and convert a value with `float( )`
 --> main.zeno:2:19
  |
2 | let real: float = whole
  |                   ^

1 error found. Nothing was run.
let whole = 3
let real: float = float(whole)
print(real, str(whole) + " items", int("42") + 1)
3.0 3 items 43

The functions int, float and str make the change, where it can be seen.

16.7 Only none can be added to a type

A type can be widened with or none, and with nothing else. A value that is either an int or a str is written as an enum whose cases carry each:

let v: int or str = 3
error: only `or none` can follow a type for now
 --> main.zeno:1:15
  |
1 | let v: int or str = 3
  |               ^
enum Setting {
    Number(n: int)
    Word(text: str)
}
let settings = [Number(3), Word("auto")]
print(settings)
[Number(3), Word("auto")]

An enum names the choices, and a match must then handle each one, which a bare int or str would not ask for.

Types go after names, name: type, and after ->. A written type is checked and never changed. Zeno works out the types of variables, returns, parameters and fields wherever they are certain, and leaves a value dynamic where they are not; any asks for that on purpose. Strict mode, from --strict or a first line strict, refuses dynamic values, and every compiled program is strict. Nothing changes type by itself.

  1. Take a program from an earlier chapter, delete every type in it, and run zeno check --strict. Which types does it ask for back?

  2. Write a function with no types and call it with an int. Then call it with a float too. What does strict mode say, and why?

  3. Give a field a written type, store a value of another type in it, and read the message.

  4. Rewrite a list[any] of numbers and words as a list of an enum, and print each item with a match.