Skip to content

Commit 1de3036

Browse files
committed
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".
1 parent 2aa8416 commit 1de3036

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/core/CCFormat.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ let break fmt (m, n) = Format.pp_print_break fmt m n
3131
let newline = Format.pp_force_newline
3232
let substring out (s, i, len) : unit = string out (String.sub s i len)
3333
let text = Format.pp_print_text
34+
let option = Format.pp_print_option
35+
let opt = option
36+
let result = Format.pp_print_result
3437

3538
let string_lines out (s : string) : unit =
3639
fprintf out "@[<v>";
@@ -88,11 +91,6 @@ let iter ?(sep = return ",@ ") pp fmt seq =
8891
sep fmt ();
8992
pp fmt x)
9093

91-
let opt pp fmt x =
92-
match x with
93-
| None -> Format.pp_print_string fmt "none"
94-
| Some x -> Format.fprintf fmt "some %a" pp x
95-
9694
let pair ?(sep = return ",@ ") ppa ppb fmt (a, b) =
9795
Format.fprintf fmt "%a%a%a" ppa a sep () ppb b
9896

src/core/CCFormat.mli

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,16 @@ val arrayi : ?sep:unit printer -> (int * 'a) printer -> 'a array printer
9999
val seq : ?sep:unit printer -> 'a printer -> 'a Seq.t printer
100100
val iter : ?sep:unit printer -> 'a printer -> 'a iter printer
101101

102-
val opt : 'a printer -> 'a option printer
103-
(** [opt pp] prints options as follows:
104-
- [Some x] will become "some foo" if [pp x ---> "foo"].
105-
- [None] will become "none". *)
102+
val option : ?none:unit printer -> 'a printer -> 'a option printer
103+
(** [opt ?none pp] prints options as follows:
104+
- [Some x] will become [pp x]
105+
- [None] will become [none ()]
106+
@since NEXT_RELEASE *)
107+
108+
val opt : ?none:unit printer -> 'a printer -> 'a option printer
109+
(** Alias of {!option} *)
110+
111+
val result : ok:'a printer -> error:'e printer -> ('a, 'e) result printer
106112

107113
(** In the tuple printers, the [sep] argument is only available.
108114
@since 0.17 *)

0 commit comments

Comments
 (0)