CCFormat(feat): add option and result, change opt

Add CCFormat.option and CCFormat.result as aliases to
Format.pp_print_option and Format.pp_print_result. Make CCFormat.opt an
alias of CCFormat.option, as such this add an optional argument to print
the case "None" but change the default behaviour. Previously, it as
printing "some _" or "none" now it print something only in the case of
"Some x" and just "x".
This commit is contained in:
Fardale 2024-12-25 14:38:38 +01:00
parent 2aa8416b1c
commit 1de3036e0c
2 changed files with 13 additions and 9 deletions

View file

@ -31,6 +31,9 @@ let break fmt (m, n) = Format.pp_print_break fmt m n
let newline = Format.pp_force_newline let newline = Format.pp_force_newline
let substring out (s, i, len) : unit = string out (String.sub s i len) let substring out (s, i, len) : unit = string out (String.sub s i len)
let text = Format.pp_print_text let text = Format.pp_print_text
let option = Format.pp_print_option
let opt = option
let result = Format.pp_print_result
let string_lines out (s : string) : unit = let string_lines out (s : string) : unit =
fprintf out "@[<v>"; fprintf out "@[<v>";
@ -88,11 +91,6 @@ let iter ?(sep = return ",@ ") pp fmt seq =
sep fmt (); sep fmt ();
pp fmt x) pp fmt x)
let opt pp fmt x =
match x with
| None -> Format.pp_print_string fmt "none"
| Some x -> Format.fprintf fmt "some %a" pp x
let pair ?(sep = return ",@ ") ppa ppb fmt (a, b) = let pair ?(sep = return ",@ ") ppa ppb fmt (a, b) =
Format.fprintf fmt "%a%a%a" ppa a sep () ppb b Format.fprintf fmt "%a%a%a" ppa a sep () ppb b

View file

@ -99,10 +99,16 @@ val arrayi : ?sep:unit printer -> (int * 'a) printer -> 'a array printer
val seq : ?sep:unit printer -> 'a printer -> 'a Seq.t printer val seq : ?sep:unit printer -> 'a printer -> 'a Seq.t printer
val iter : ?sep:unit printer -> 'a printer -> 'a iter printer val iter : ?sep:unit printer -> 'a printer -> 'a iter printer
val opt : 'a printer -> 'a option printer val option : ?none:unit printer -> 'a printer -> 'a option printer
(** [opt pp] prints options as follows: (** [opt ?none pp] prints options as follows:
- [Some x] will become "some foo" if [pp x ---> "foo"]. - [Some x] will become [pp x]
- [None] will become "none". *) - [None] will become [none ()]
@since NEXT_RELEASE *)
val opt : ?none:unit printer -> 'a printer -> 'a option printer
(** Alias of {!option} *)
val result : ok:'a printer -> error:'e printer -> ('a, 'e) result printer
(** In the tuple printers, the [sep] argument is only available. (** In the tuple printers, the [sep] argument is only available.
@since 0.17 *) @since 0.17 *)