From 4ca1295c44ff6f617e6250210277fdbe00ba0596 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 11 Jul 2014 23:05:13 +0200 Subject: [PATCH] CCError.fail_printf --- core/CCError.ml | 6 ++++++ core/CCError.mli | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/core/CCError.ml b/core/CCError.ml index fe1fdd86..851cd5c1 100644 --- a/core/CCError.ml +++ b/core/CCError.ml @@ -43,6 +43,12 @@ let return x = `Ok x let fail s = `Error s +let fail_printf format = + let buf = Buffer.create 16 in + Printf.kbprintf + (fun buf -> fail (Buffer.contents buf)) + buf format + let _printers = ref [] let register_printer p = _printers := p :: !_printers diff --git a/core/CCError.mli b/core/CCError.mli index ab850d9a..d054296d 100644 --- a/core/CCError.mli +++ b/core/CCError.mli @@ -45,6 +45,11 @@ val fail : string -> 'a t val of_exn : exn -> 'a t +val fail_printf : ('a, Buffer.t, unit, 'a t) format4 -> 'a +(** [fail_printf format] uses [format] to obtain an error message + and then returns [`Error msg] + @since NEXT_VERSION *) + val map : ('a -> 'b) -> 'a t -> 'b t val map2 : ('a -> 'b) -> (string -> string) -> 'a t -> 'b t