The Zeno Book Zeno 0.1 zeno-lang.org

13 Values That May Be Missing

Sometimes there is no answer. A search finds nothing, a sensor has not reported yet, a line that should hold a number holds a word. Zeno has one value for "nothing here": none. This chapter is about where it may appear, and how Zeno makes sure a program never uses it by mistake.

13.1 none only where the type says so

A value can be none only if its type says it may be. The type is written T or none, and reads as it means:

function find_user(id: int) -> str or none {
    if id == 1 {
        return "ada"
    }
    return none
}

print(find_user(1), find_user(2))
ada none

A plain str is always a piece of text. A str or none is a piece of text or nothing, and the program has to find out which before it treats it as text:

function find_user(id: int) -> str or none {
    if id == 1 {
        return "ada"
    }
    return none
}

print(find_user(1).upper())
error: this is str or none. Check it with `is not none` before using `.upper`
 --> main.zeno:8:7
  |
8 | print(find_user(1).upper())
  |       ^

1 error found. Nothing was run.

Using a null value is the commonest crash in C, Java and JavaScript. Tony Hoare, who brought null into programming in 1965, called it his "billion-dollar mistake". In those languages any reference can be null, so every use is a risk. In Zeno only a value whose type says or none can be, and the check cannot be forgotten. (Decisions 1.13 and 3.12.)

13.2 Asking: is none, is not none

x is none and x is not none ask the question. Inside an if that has found the value is there, Zeno knows it, and the value can be used as a plain value:

function find_user(id: int) -> str or none {
    if id == 1 {
        return "ada"
    }
    return none
}

for id in [1, 2] {
    let user = find_user(id)
    if user is not none {
        print(user.upper())
    } else {
        print("no user", id)
    }
}
ADA
no user 2

The question is always asked with is. == none is refused, with the spelling to use, and so is asking about a value that can never be none, because the answer would always be false:

let count = 3
if count is none {
    print("no count")
}
error: `is none` is always false: this is int, which is never `none`. Only a value whose type says `or none` can be
 --> main.zeno:2:4
  |
2 | if count is none {
  |    ^

1 error found. Nothing was run.

There is no new syntax to learn for this. The program writes the if it would have written anyway, and the checker follows it. This is called narrowing: inside the if, the type of user is narrowed from str or none to str.

is is only for none. Values are compared with ==:

let x = 3
print(x is 3)
error: `is` only tests for `none`. Use `==` to compare values
 --> main.zeno:2:12
  |
2 | print(x is 3)
  |            ^

In Python, is asks whether two names are one object, which gives surprising answers on numbers and text and is a well-known trap. Keeping is for none leaves one way to compare values and one way to ask whether something is there. (Decision 1.13.)

13.3 What the checker follows

The checker follows the ordinary shapes a program takes.

Leaving early.

After if x is none { return ... }, the rest of the block can only be reached when x is there:

function shout(text: str or none) -> str {
    if text is none {
        return ""
    }
    return text.upper()
}
print(shout("hi"), shout(none))
HI 

The same goes for raise, break and continue.

and, or and not.

A test joined with and knows the first half when it asks the second:

let name: str or none = "Ada"
if name is not none and len(name) > 2 {
    print(name, "is long enough")
}
Ada is long enough

Filling in a default.

After an if that gives the variable a value when it had none, the variable has a value on both paths, and the checker knows it:

function greeting(given: str or none) -> str {
    var name = given
    if name is none {
        name = "friend"
    }
    return f"Hello, {name.upper()}"
}
print(greeting("Ada"), greeting(none))
Hello, ADA Hello, FRIEND

Walking a chain.

A while that asks is not none knows it inside, and an assignment in the body makes the variable uncertain again until the next question:

class Node {
    value: int
    next: Node or none = none
}

var node: Node or none = Node(1, Node(2, Node(3)))
var total = 0
while node is not none {
    total += node.value
    node = node.next
}
print(total)
6

Without these, the two most ordinary uses of none, walking a linked list and filling in a default, would not pass the checker, and the program would have to copy the value into a second variable just to please it. TypeScript and Kotlin follow assignments the same way. (Decision 3.12.)

A field is asked about through a name

The checker narrows names, not fields. A field that may be none is first given a name of its own, and the name is asked about:

class Sensor {
    last: float or none = none
}
let s = Sensor()
s.last = 21.5
if s.last is not none {
    print(s.last + 1.0)
}
error: `+` does not work between float or none and float. `last` is a field, and a field can be given a new value between a test and its use, so a test of it is not remembered. Copy it into a variable with `let`, and test that for `none`
 --> main.zeno:7:11
  |
7 |     print(s.last + 1.0)
  |           ^

1 error found. Nothing was run.
class Sensor {
    last: float or none = none
}
let s = Sensor()
s.last = 21.5
let last = s.last
if last is not none {
    print(last + 1.0)
}
22.5

A field can be changed by any code that holds the object, including a method called between the question and the use, so an answer about a field could be out of date by the next line. A local name cannot be changed behind the program’s back.

13.4 Functions that may give none

The library gives none where there may be no answer: find on text and lists (Text), get on a dict (Dicts) and input() when there are no more lines (Reading and Writing). Your own functions do the same by saying -> T or none.

A function that says it may give none gives none when it reaches its end, since that is one of the answers it promised:

function first_even(xs: list[int]) -> int or none {
    for x in xs {
        if x % 2 == 0 {
            return x
        }
    }
}
print(first_even([1, 3, 4, 5]), first_even([1, 3]))
4 none

13.5 none in a match

A match on a value that may be none can have case none, and every case after it knows the value is there. For an enum, case none and a case for each of the enum’s own cases make the match complete:

enum Reading {
    Value(volts: float)
    Fault(code: int)
}

function describe(r: Reading or none) -> str {
    match r {
        case none { return "no reading yet" }
        case Value(v) { return f"{v:.1f} V" }
        case Fault(code) { return f"fault 0x{code:02x}" }
    }
}

print(describe(none), describe(Value(3.3)), describe(Fault(7)))
no reading yet 3.3 V fault 0x07

Leave case none out, and the checker says so:

enum Reading {
    Value(volts: float)
    Fault(code: int)
}

function describe(r: Reading or none) -> str {
    match r {
        case Value(v) { return "value" }
        case Fault(code) { return "fault" }
    }
}
error: this `match` does not handle `none`. Add a `case` for it, or a final `case _`
 --> main.zeno:7:5
  |
7 |     match r {
  |     ^

1 error found. Nothing was run.

13.6 none is not zero

none is not 0, not false and not "". It is its own value, and the only things that can be done with it are to print it, store it where the type allows, and ask whether a value is it. Conditions must be bool, so if user is refused, and the question is always written out: if user is not none.

none means "nothing here", and only a value whose type says or none can hold it. Before such a value is used, the program asks is none or is not none, and the checker follows the answer through if, early returns, and, defaults and while. Fields are asked about through a name of their own. A match can have case none. is is for none and nothing else.

  1. Write function parse_volts(text: str) -> float or none that gives none for an empty text, and use it on a list of texts, printing a dash for each none.

  2. Write a function that gives the last Node of a chain, or none for an empty one.

  3. Take the greeting function and remove the if. Read the message, and say in your own words what it protects against.

  4. Write function largest(xs: list[int]) -> int or none with no max. What should it give for [], and why is that better than 0?