soma.cell

Null Handling

() is null in Soma. The ?? operator provides defaults for null values.

Example 007 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.

How it is built

The cell NullHandling has no face and no memory. Handlers: run (line 15).

Generated from soma describe on this file.

The source

007_null_handling.cell

// ============================================================================
// 007 — Null Handling
// ============================================================================
// () is null in Soma. The ?? operator provides defaults for null values.
//
// Run:   soma run 007_null_handling.cell run
// Expect:
//   value is null: true
//   with default: 42
//   name: Unknown
//   found: Alice

cell NullHandling {

    on run() {
        // () is the null / unit value
        let value = ()

        // Check for null with == ()
        if value == () {
            print("value is null: true")
        }

        // ?? is the null-coalescing operator: returns left if non-null, else right
        let result = value ?? 42
        print("with default: {result}")

        // Practical example: missing map field returns ()
        let user = map("age", 25)
        let name = user.name ?? "Unknown"
        print("name: {name}")

        // When value exists, ?? returns the original value
        let user2 = map("name", "Alice", "age", 30)
        let name2 = user2.name ?? "Unknown"
        print("found: {name2}")

        return "done"
    }
}

What the toolchain says

$ soma check 007_null_handling.cell
✓ All checks passed.
$ soma run 007_null_handling.cell run
value is null: true
with default: 42
name: Unknown
found: Alice
done