lloyAlloy on GitHub
Conditional bindings
Docs

Conditional bindings

Bindings in conditions, narrowed where they live

Updated

if local, elseif local, while local, and the expression form bind a value and test it in one step. The branch declares the name from the tested temp. Inside the branch it is T, not T?, and closures see the same.

Alloybindings.aly
1if local player = first_player() then
2    print(player.Name)
3end
4while local job = table.remove(queue) do
5    run(job)
6end
Emitted Luausame line count
1do local _c1 = first_player() if _c1 then local player = _c1
2    print(player.Name)
3end end
4while true do local _c1 = table.remove(queue) if not (_c1) then break end local job = _c1
5    run(job)
6end return nil
  • where adds a filter: if local p = f() where p.AccountAge > 30 then.
  • A loop filters too: for _, { x, y } in points where x > 0 do skips an item the condition rejects, and the head destructures each one.
  • Several bindings chain: if local p = a; local c = p.Character then runs the second only when the first is truthy.
  • Pattern forms bind too: if local Ok(v) = result then, while local Move(target) = next() do.
  • if not local x = f() then return end is the guard clause; x stays in scope after it.