1234567891011121314151617181920212223242526272829303132333435(* belongs in Common, but moved here to avoid circular dependencies *)open!Importtype'areturn={return:'b.'a->'b}[@@unboxed]letwith_return(typea)f=letmoduleM=struct(* Raised to indicate ~return was called. Local so that the exception is tied to a
particular call of [with_return]. *)exceptionReturnofaendinletis_alive=reftrueinletreturna=ifnot!is_alivethenfailwith"use of [return] from a [with_return] that already returned";Exn.raise_without_backtrace(M.Returna);intryleta=f{return}inis_alive:=false;awithexn->is_alive:=false;matchexnwith|M.Returna->a|_->raiseexn;;letwith_return_optionf=with_return(funreturn->f{return=funa->return.return(Somea)};None);;letprepend{return}~f={return=funx->return(fx)}