Module Base.Printf

Functions for formatted output.

fprintf and related functions format their arguments according to the given format string. The format string is a character string which contains two types of objects: plain characters, which are simply copied to the output channel, and conversion specifications, each of which causes conversion and printing of arguments.

Conversion specifications have the following form:

% [flags] [width] [.precision] type

In short, a conversion specification consists in the % character, followed by optional modifiers and a type which is made of one or two characters.

The types and their meanings are:

The optional flags are:

The optional width is an integer indicating the minimal width of the result. For instance, %6d prints an integer, prefixing it with spaces to fill at least 6 characters.

The optional precision is a dot . followed by an integer indicating how many digits follow the decimal point in the %f, %e, and %E conversions. For instance, %.4f prints a float with 4 fractional digits.

The integer in a width or precision can also be specified as *, in which case an extra integer argument is taken to specify the corresponding width or precision. This integer argument precedes immediately the argument to print. For instance, %.*f prints a float with as many fractional digits as the value of the argument given before the float.

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.Caml.Buffer.t -> ('rBase__.Import0.Caml.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.Caml.Buffer.t -> 'a) -> Base__.Import0.Caml.Buffer.t -> ('rBase__.Import0.Caml.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.