veko-lang.org
Download Free EPUB 3.0

Part I · Mental Model & Rosetta Stone

Chapter 3

Zero-`nil` Error Recovery, Refinements & ORO Memory

User-declarable /refinements, contextual /or [e -> ...] recovery, LIFO defer, and in-place mut byte slabs

MEMORY MODEL

ORO Single-Owner Memory: Borrow, Move, mut Loan & Copy-on-Write

Rust-grade deterministic cleanup and zero garbage collector pauses without lifetime annotations.

ZERO-COPY FAST PATH (RC == 1) 99% of calls & mutations SHARED SPLIT & SCOPE EXIT Cycle-free DAG + zeroize 16B header rc == 1 bind / return mut when rc > 1 scope exit old rc-- Allocation1. Create ValueSSO <=14B or Unique Slab Fast Call2. Read-Only CallZero-Cost Borrow In-Place Mutation3. Explicit mut Loanmut param + mut call site Move / Share4. Binding & ReturnOwnership Transfer Copy-on-Write5. Lazy CoW Splitensure_unique on rc > 1 Cleanup6. Deterministic DropFree-List + .secret Wipe
Component & ModuleArchitectural Role
1. Create Value
runtime.vk + native_rt.vk
Allocates scalars and <=14B strings/bytes inline in 16B Value; larger data gets rc=1 slab.
2. Read-Only Call
vko_emit.vk + native_emit.vk
Normal function parameters borrow the 16-byte Value header with zero refcount traffic.
3. Explicit mut Loan
types.vk + native_emit.vk
Mutates uniquely owned buffers, blocks, maps, and struct fields directly in place.
4. Binding & Return
oro_vm.vk + native_rt.vk
Rebinding or returning transfers ownership cleanly; aliasing increments non-atomic rc.
5. Lazy CoW Split
runtime.vk + native_rt.vk
Clones the backing slab only at the exact moment a shared container is mutated.
6. Deterministic Drop
runtime.vk + native_rt.vk
Reclaims dead slabs immediately at lexical scope exit or /or unwind.

Recipe 3.1: User-Declarable Slash Refinements & Contextual /or [e -> ...]

Any function can declare optional /refinement modifiers with defaults, and callers can combine refinements in left-to-right order plus attach /or error recovery:

VEKO
dial-upstream = fn [host str /port p i64 = 443 /tls secure bool = true] str [
    if host.trim == '' [ return (err 'upstream host cannot be empty') ]
    scheme = if secure [ 'https' ] else [ 'http' ]
    `{scheme}://{host.trim}:{p}`
]

// Base arity uses defaults (p = 443, secure = true)
prod_url = dial-upstream 'veko-lang.org'

// Extend arity cleanly at the call site via /port/tls
dev_url = dial-upstream/port/tls '127.0.0.1' 8080 false

// Recover inline and inspect the caught error `e`
safe_url = dial-upstream/or '' [e ->
    println `Falling back to loopback ({mold e})`
    'http://127.0.0.1:8080'
]

Recipe 3.2: Deterministic LIFO defer Cleanup on Return and /or Unwind

Pair every file handle, socket, or temporary directory with defer [ ... ] on the line immediately below acquisition so cleanup runs in LIFO order on both normal return and early error returns:

VEKO
use [fs]

write-verified-report = fn [dst_path str, lines block[str]] any [
    tmp_dir = str/join [(os/temp-dir) '/report_stage_' (mold (time/unix))] ''
    _ = fs/mkdir-all tmp_dir
    defer [ _ = fs/remove-all tmp_dir ]

    stage_file = `{tmp_dir}/draft.txt`
    payload = bytes/from-str (str/join lines '\n')
    _ = fs/write-atomic/or stage_file payload [e ->
        return (err `staging write failed: {mold e}`)
    ]
    fs/copy-file stage_file dst_path
]

Recipe 3.3: In-Place Byte Slab Building & Disjoint Field Loans (mut)

Pre-allocate binary packet buffers once with bytes/with-cap and pass explicit mut loans (including disjoint struct field loans like mut state.tx_buf) for zero-copy framing:

VEKO
PacketState = struct {
    stream_id i64
    tx_buf    bytes
}

append-tlv-frame = fn [mut buf bytes, tag i64, body bytes] [
    _ = bytes/push mut buf (tag & 0xff)
    _ = bytes/push-u16-be mut buf (len body)
    _ = bytes/push-all mut buf body
    .ok
]

build-hello-packet = fn [] bytes [
    mut st = PacketState {
        stream_id = 1
        tx_buf    = (bytes/with-cap 128)
    }
    append-tlv-frame mut st.tx_buf 0x01 (bytes/from-str 'VEKO-1.0')
    append-tlv-frame mut st.tx_buf 0x02 (bytes/from-str 'veko-lang.org')
    st.tx_buf
]