typestar

The context system in Odin

Every proc carries an implicit context; the temp allocator is its cheapest trick.

package main

import "core:fmt"

main :: proc() {
    // Every proc carries an implicit context with two allocators.
    fmt.println("default:", context.allocator != {})

    // tprintf builds strings on the temp allocator -- no delete needed.
    tag := fmt.tprintf("%s berth %d", "Valparaiso", 9)
    fmt.println("tag:", tag)

    // One call reclaims everything temporary at a natural boundary.
    free_all(context.temp_allocator)
    fmt.println("temp storage swept")
}

How it works

  1. context.allocator and context.temp_allocator ride along on every call.
  2. fmt.tprintf builds a string on temp storage — nothing to delete.
  3. free_all(context.temp_allocator) sweeps it all at a natural boundary.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
442
Tokens
73
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 67 seconds.

Type this snippet

Step 3 of 3 in Idioms, step 18 of 27 in Language basics.

← Previous Next →