Skip to content

Commit

Permalink
CCFormat(feat): add option and result, change opt
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
FardaleM committed Dec 25, 2024
1 parent 07f7ce6 commit 6953e5a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/core/CCFormat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ let break fmt (m, n) = Format.pp_print_break fmt m n
let newline = Format.pp_force_newline
let substring out (s, i, len) : unit = string out (String.sub s i len)
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 =
fprintf out "@[<v>";
Expand Down Expand Up @@ -88,11 +91,6 @@ let iter ?(sep = return ",@ ") pp fmt seq =
sep fmt ();
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) =
Format.fprintf fmt "%a%a%a" ppa a sep () ppb b

Expand Down
14 changes: 10 additions & 4 deletions src/core/CCFormat.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 iter : ?sep:unit printer -> 'a printer -> 'a iter printer

val opt : 'a printer -> 'a option printer
(** [opt pp] prints options as follows:
- [Some x] will become "some foo" if [pp x ---> "foo"].
- [None] will become "none". *)
val option : ?none:unit printer -> 'a printer -> 'a option printer
(** [opt ?none pp] prints options as follows:
- [Some x] will become [pp x]
- [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.
@since 0.17 *)
Expand Down

0 comments on commit 6953e5a

Please sign in to comment.