Protected calls let code choose a fallback value after a failure.

fallback A fallback should be explicit so later code can tell the recovery path from a successful calculation.

Fallback Result

raw
fallback_result.lua
Replay: real traced execution (multi-file project)
local raw = "5"
local result = tonumber(raw)
local ok = result ~= nil
local value = 0

if ok then
  value = result
end

print("raw=" .. raw)
print("ok=" .. tostring(ok))
print("value=" .. value)
local raw = "bad"
local result = tonumber(raw)
local ok = result ~= nil
local value = 0

if ok then
  value = result
end

print("raw=" .. raw)
print("ok=" .. tostring(ok))
print("value=" .. value)
local raw = "11"
local result = tonumber(raw)
local ok = result ~= nil
local value = 0

if ok then
  value = result
end

print("raw=" .. raw)
print("ok=" .. tostring(ok))
print("value=" .. value)
  1. raw ← 5, result ← 5, ok ← true, value ← 0

    1local raw→ 5 = "5" --@raw="bad", "11"2local result→ 5 = tonumber(raw5)3local ok→ true = result5 ~= nil4local value→ 0 = 0
  2. value ← 5

    6if oktrue then7  value→ 5 = result58end
  3. print("raw=" .. raw)

    10print("raw=" .. raw5)11print("ok=" .. tostring(oktrue))12print("value=" .. value5)
    outputraw=5
    ok=true
    value=5
  1. raw ← bad, result ← nil, ok ← false, value ← 0

    1local raw→ bad = "bad"2local result→ nil = tonumber(rawbad)3local ok→ false = resultnil ~= nil4local value→ 0 = 056if ok then7  value = result8end910print("raw=" .. rawbad)11print("ok=" .. tostring(okfalse))12print("value=" .. value0)
    outputraw=bad
    ok=false
    value=0
  1. raw ← 11, result ← 11, ok ← true, value ← 0

    1local raw→ 11 = "11"2local result→ 11 = tonumber(raw11)3local ok→ true = result11 ~= nil4local value→ 0 = 0
  2. value ← 11

    6if oktrue then7  value→ 11 = result118end
  3. print("raw=" .. raw)

    10print("raw=" .. raw11)11print("ok=" .. tostring(oktrue))12print("value=" .. value11)
    outputraw=11
    ok=true
    value=11