package core

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
val ifprintf : 'a -> ('r, 'a, 'c, unit) Stdlib.format4 -> 'r

Same as fprintf, but does not print anything. Useful for ignoring some material when conditionally printing.

val sprintf : ('r, unit, string) Stdlib.format -> 'r

Same as fprintf, but instead of printing on an output channel, returns a string.

val bprintf : Base__.Import0.Stdlib.Buffer.t -> ('r, Base__.Import0.Stdlib.Buffer.t, unit) Stdlib.format -> 'r

Same as fprintf, but instead of printing on an output channel, appends the formatted arguments to the given extensible buffer.

val ksprintf : (string -> 'a) -> ('r, unit, string, 'a) Stdlib.format4 -> 'r

Same as sprintf, but instead of returning the string, passes it to the first argument.

val kbprintf : (Base__.Import0.Stdlib.Buffer.t -> 'a) -> Base__.Import0.Stdlib.Buffer.t -> ('r, Base__.Import0.Stdlib.Buffer.t, unit, 'a) Stdlib.format4 -> 'r

Same as bprintf, but instead of returning immediately, passes the buffer, after printing, to its first argument.

Formatting error and exit functions

These functions have a polymorphic return type, since they do not return. Naively, this doesn't mix well with variadic functions: if you define, say,

let f fmt = ksprintf (fun s -> failwith s) fmt

then you find that f "%d" : int -> 'a, as you'd expect, and f "%d" 7 : 'a. The problem with this is that 'a unifies with (say) int -> 'b, so f "%d" 7 4 is not a type error -- the 4 is simply ignored.

To mitigate this problem, these functions all take a final unit parameter. These rarely arise as formatting positional parameters (they can do with e.g. "%a", but not in a useful way) so they serve as an effective signpost for "end of formatting arguments".

val failwithf : ('r, unit, string, unit -> _) Stdlib.format4 -> 'r

Raises Failure.

val invalid_argf : ('r, unit, string, unit -> _) Stdlib.format4 -> 'r

Raises Invalid_arg.

val eprintf : ('a, Stdlib.out_channel, Base.Unit.t) Stdlib.format -> 'a
val fprintf : Stdlib.out_channel -> ('a, Stdlib.out_channel, Base.Unit.t) Stdlib.format -> 'a
val kfprintf : (Stdlib.out_channel -> 'a) -> Stdlib.out_channel -> ('b, Stdlib.out_channel, Base.Unit.t, 'a) Stdlib.format4 -> 'b
val printf : ('a, Stdlib.out_channel, Base.Unit.t) Stdlib.format -> 'a
val exitf : ('a, Base.Unit.t, Base.String.t, Base.Unit.t -> _) Stdlib.format4 -> 'a

print to stderr; exit 1

type printf = {
  1. printf : 'a. ('a, Stdlib.Buffer.t, Base.Unit.t) Stdlib.format -> 'a;
}
val collect_to_string : (printf -> Base.Unit.t) -> Base.String.t

collect_to_string (fun { printf } -> ...) lets you easily convert code that was printing to stdout into code that produces a string.

For example, this original code...

printf "hello ";
(* long computation *)
printf "%s%c" "world" '!'

... can be wrapped like so.

Printf.collect_to_string (fun { printf } ->
  printf "hello ";
  (* long computation *)
  printf "%s%c" "world" '!')

The above is easier than manually editing many lines of the original:

let hello = sprintf "hello " in
(* long computation *)
let world = sprintf "%s%c" "world" '!' in
hello ^ world
OCaml

Innovation. Community. Security.