Supervisor
Automatic retry with a limit using the supervised function from Std.Supervisor.
If the computation fails, it restarts up to N times before giving up.
import Std.Fail (Fail)
import Std.Result
import Std.Supervisor (supervised)
fun unreliable : Unit -> Int needs {Fail String}
unreliable () = fail! "something went wrong"
main () = {
let r1 = supervised 3 (fun () -> unreliable ())
dbg $"unreliable: {debug r1}"
}Output:
unreliable: Err("something went wrong")
supervised 3 runs the function and retries up to 3 times on failure. If all
attempts fail, it returns the last error as an Err. If any attempt succeeds,
it returns Ok with the value.
This is built on the same effect handler patterns described in
Supervision. The supervised function is a convenience
wrapper around a handler that catches Fail and re-runs the computation.