Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Optionals and None

None is the value Acton uses for "nothing here".

An optional type ?T means a value is either a T or None. Use an optional when absence is part of the normal result.

`?str` means "a string or None". `?int` means "an integer or None". The `?` belongs to the type, not the value.

def lookup_name(users: dict[str, str], username: str) -> ?str:
    if username in users:
        return users[username]
    return None

actor main(env):
    users = {"alice": "Alice Andersson"}

    name = lookup_name(users, "bob")
    if name is not None:
        print("Found:", name)
        print("Upper:", name.upper())
    else:
        print("No match")

    env.exit(0)

Use is None and is not None to check whether an optional is present. When you want to use the value as non-optional, put that code inside the is not None branch.

Write the check in the direction of the code that needs the value. Do not rely on an if name is None / else shape to make name non-optional in the else branch.

Optional values are common in lookups, parsing, and APIs that may or may not find a result. None is not a general placeholder for every kind of empty value; use it when absence itself matters.