package batteries
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=d3d16ffc960e832a1c0e21b7123194e9
sha512=36c8561079aa9fefc864c2f03bd8ae20aa05987bb67e7a9beac3ab4f440dbf7d44f141c172b05fddb1a5b618fc55a27962bb45fc39bd7b3a15d56e0ed3ff3870
doc/batteries.unthreaded/BatReturn/index.html
Module BatReturn
Local exceptions/labels/goto/return.
This module defines a mechanism akin to SML's exception generators or to a generalization of C's return, i.e. the ability to define local labels, which may be used for immediately terminating an expression and returning a value. By opposition to usual OCaml exceptions, this mechanism
- allows polymorphic return values
- makes accidental exception catching slightly harder (while a local exception can escape its scope, it cannot be caught again by accident from this module).
Example:
let find_in_array a e =
label (fun label ->
for i = 0 to Array.length a - 1 do
if Array.get a i = e then return label (Some i)
done;
None)@documents Return
val label : ('a t -> 'a) -> 'alabel f creates a new label x and invokes f x. If, during the execution of f, return x v is invoked, the execution of f x stops immediately and label f returns v. Otherwise, if f x terminates normally and returns y, label f returns y.
Calling return x v from outside scope f is a run-time error and causes termination of the program.
val with_label : ('a t -> 'a) -> 'aas label
val return : 'a t -> 'a -> _Return to a label. return l v returns to the point where label l was obtained and produces value l.
Calling return l v from outside the scope of l (i.e. the call to function label which produced l) is a run-time error and causes termination of the program.