7. Declare your own effect
Teaches: effect decl, perform, handle end-to-end
Effects aren't limited to the built-ins. effect Net { fetch : Int -> Int }
declares a new effect with one operation; with Net as net { fetch(n) => … }
installs a handler for it, naming a capability (net) that the program
uses to perform fetch calls. This is the moat feature: user-defined
effects, not just user-defined data.
effect Net { fetch : Int -> Int }
handle
(net.fetch(1)) + (net.fetch(2))
with Net as net {
fetch(n) => n * 10
}Expected output (bang run stdout):
30Run it yourself:
curl -fsSL https://raw.githubusercontent.com/phibkro/bang/main/tools/install.sh | sh
bang run examples/handle-custom-tracer/main.bang← 6. Handling an effect · 8. Swap the handler, keep the program →