- Lua 100%
luffect #
A Lua port of Effect — specifically the v4 core (effect-smol): structured errors, Option/Result, structural equality, and a fiber-based effect runtime with coroutine do-notation.
Runs on Lua 5.1+, Lua 5.4, and LuaJIT.
local Effect = require("luffect.Effect")
local Option = require("luffect.Option")
local program = Effect.gen(function()
local a = coroutine.yield(Effect.succeed(1))
local b = coroutine.yield(Option.some(2)) -- Options are Effectable
local c = coroutine.yield(Effect.sync(function() return a + b end))
return c * 10
end)
print(Effect.runSync(program)) --> 30
Errors are structured (v4's flat Cause model — expected Fails vs.
defect Dies), and Lua error()s inside effects become defects:
local result = Effect.fail("boom"):pipe(
Effect.catch(function(e) return Effect.succeed("caught " .. e) end)
)
print(Effect.runSync(result)) --> caught boom
Status #
Early days. Ported so far: Function (pipe/flow/dual), Pipeable,
Predicate, Ordering/Order/Equivalence, Hash/Equal, Option,
Result, Cause/Exit, the Effect fiber runtime (gen, error
handling), and the async runtime: Effect.callback, structured
interruption with finalizers (ensuring/onExit/onInterrupt),
cooperative yielding, Effect.sleep/delay on uv timers (real or
virtual via Clock.makeTest()), runFork/runCallback/runMain,
and Fiber.await/join/interrupt. Schedulers are auto-detected.
Roots start inline and raw Effect.callback resumes in its caller's
context. Built-in Clock and uv adapters hop Neovim fast-event callbacks
through vim.schedule, so their continuations can safely call vim.api:
-- in a plugin
local fiber = Effect.runFork(Effect.gen(function()
coroutine.yield(Effect.sleep(100))
vim.api.nvim_echo({ { "tick" } }, false, {}) -- main loop: api-safe
return "done"
end))
-- in a script (nvim -l or plain lua with luv): pump the loop
Effect.runMain(Effect.sleep(100))
Structured concurrency is in: supervised Effect.fork (parents
interrupt children when they settle), Effect.race, Effect.all/
forEach with { concurrency = n | "unbounded" }, and Deferred/
Latch/Semaphore/Queue for coordination (queues carry the v4
completion model: backpressure, dropping/sliding/rendezvous
strategies, Queue.end_ draining into Cause.Done) — all
deterministic under tests via Clock.makeTest() and the sync
scheduler.
Resource safety is in too: Effect.scoped + Effect.acquireRelease
guarantee finalizers run (LIFO) on success, failure, and
interruption — uv handles opened inside a scope get closed no matter
how the fiber ends — plus Ref/SynchronizedRef for state.
Dependency injection works the v4 way: define a Context.Key, yield
it inside gen to get the service, and swap implementations with
Effect.provideService — the same program runs against vim.api in
the editor and a captured double in tests. The runtime's own services
(clock, scheduler, op budget, scope) are References on the context,
so Effect.withClock(eff, Clock.makeTest()) virtualizes time for any
region. Layer wires whole service graphs: memoized construction
(shared dependencies build once), resource-safe teardown in
dependency order, composed with Layer.provide/merge and applied
with Effect.provide(program, AppLayer). Duration rounds out time
handling (Effect.sleep({ minutes = 1, seconds = 30 })).
Resilience and ergonomics round it out: Effect.retry with
Schedule policies (exponential backoff, fixed intervals, jitter),
Effect.repeat_, Effect.timeout (interrupting the slow side),
Data.TaggedError + Effect.catchTag for typed error handling,
Match for pattern matching, and Array utilities:
Effect.catchTag(
Effect.timeout(Effect.retry(flaky, Schedule.exponential(100)), { seconds = 5 }),
"TimeoutError",
function() return Effect.succeed(fallback) end
)
Stream brings pull-based streaming with resource safety —
Stream.callback turns any uv callback source into a stream, and
early termination (take) closes the underlying handles:
local ticks = Stream.callback(function(emit)
local t = vim.uv.new_timer()
t:start(100, 100, function() emit.single(os.time()) end)
return Effect.sync(function() t:stop(); t:close() end)
end)
Effect.runFork(ticks:pipe(Stream.take(10), Stream.runForEach(print)))
ManagedRuntime is the plugin-lifecycle shape: build your service
graph once from a Layer, runtime:runFork(...) in handlers, and
runtime:disposeEffect() on unload interrupts in-flight fibers and
tears every resource down.
See doc/recon0.md for the current architectural assessment, upstream alignment, correction priorities, and next exploration map. doc/porting.md records architecture notes and porting conventions (core port complete; forward work lives in beads and state-life.md), doc/async.md for the async strategy, and doc/caveats.md for discrepancies vs upstream.
Tests #
busted # or: busted --lua=/usr/bin/lua5.1 / lua5.4 / luajit