← Back to Demos & Examples

05_closures_and_capabilities.vk

Raw .vk
// Example 05: Stateful ORO closures and explicit capability records (non-OOP polymorphism)
// Track A (ORO VM) : veko run examples/05_closures_and_capabilities.vk
// Track A (.vko)   : veko compile --vko examples/05_closures_and_capabilities.vk -o dist/05.vko ; veko run dist/05.vko
// Track B (Wasm 3) : veko run --wasm examples/05_closures_and_capabilities.vk
// Track C (Native) : veko compile --native examples/05_closures_and_capabilities.vk -o dist/05.exe ; ./dist/05.exe
use [str]

// Stateful generator owning its private ORO capture slab
make-seq = fn [start] [
	mut cur = start
	fn [step] [
		out = cur
		cur = cur + step
		out
	]
]

next-port = make-seq 8000
println (next-port 10)
println (next-port 10)
println (next-port 10)

// Explicit capability record built from ORO closures (no classes or inheritance)
make-logger = fn [prefix] [
	{
		info = fn [msg] [println (str/append prefix msg)]
		warn = fn [msg] [println (str/append '[WARN] ' msg)]
		flush = fn [] [println (str/append prefix 'flushed')]
	}
]

app-log = make-logger '[veko] '
log-info = app-log.info
log-warn = app-log.warn
(log-info 'initialized')
(log-warn 'cache-miss')
call app-log.flush