Module Base.With_return
with_return f allows for something like the return statement in C within f.
There are three ways f can terminate:
- If 
fcallsr.return x, thenxis returned bywith_return. - If 
fevaluates to a valuex, thenxis returned bywith_return. - If 
fraises an exception, it escapeswith_return. 
Here is a typical example:
let find l ~f =
  with_return (fun r ->
    List.iter l ~f:(fun x -> if f x then r.return (Some x));
    None
  )It is only because of a deficiency of ML types that with_return doesn't have type:
val with_return : 'a. (('a -> ('b. 'b)) -> 'a) -> 'a but we can slightly increase the scope of 'b without changing the meaning of the type, and then we get:
type 'a return = { return : 'b . 'a -> 'b }
val with_return : ('a return -> 'a) -> 'aBut the actual reason we chose to use a record type with polymorphic field is that otherwise we would have to clobber the namespace of functions with return and that is undesirable because return would get hidden as soon as we open any monad. We considered names different than return but everything seemed worse than just having return as a record field. We are clobbering the namespace of record fields but that is much more acceptable.
val with_return : ('a return -> 'a) -> 'aval with_return_option : ('a return -> unit) -> 'a optionNote that
with_return_optionallocates ~5 words more than the equivalentwith_returncall.
val prepend : 'a return -> f:('b -> 'a) -> 'b returnprepend a ~freturns a valuexsuch that each call tox.returnfirst appliesfbefore applyinga.return. The call tofis "prepended" to the call to the originala.return. A possible use case is to handxover to another function which returns'b, a subtype of'a, or to capture a common transformationfapplied to returned values at several call sites.