From 48094c99892591a0df71f96aef51f51b196de68d Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Wed, 24 Oct 2018 17:17:18 +0200 Subject: [PATCH] Stdlib: add `Option.pp` --- src/lib_stdlib/option.ml | 7 +++++++ src/lib_stdlib/option.mli | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/src/lib_stdlib/option.ml b/src/lib_stdlib/option.ml index 11ce55368..e0e44d6f2 100644 --- a/src/lib_stdlib/option.ml +++ b/src/lib_stdlib/option.ml @@ -2,6 +2,7 @@ (* *) (* Open Source License *) (* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. *) +(* Copyright (c) 2019 Nomadic Labs, *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) @@ -59,3 +60,9 @@ let try_with f = try Some (f ()) with _ -> None let some x = Some x + +let pp ?(default="") data_pp ppf opt = + unopt_map + ~f:(fun i -> data_pp ppf i) + ~default:(Format.pp_print_string ppf default) + opt diff --git a/src/lib_stdlib/option.mli b/src/lib_stdlib/option.mli index 7fc49c665..64d9ab4bf 100644 --- a/src/lib_stdlib/option.mli +++ b/src/lib_stdlib/option.mli @@ -2,6 +2,7 @@ (* *) (* Open Source License *) (* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. *) +(* Copyright (c) 2019 Nomadic Labs, *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) @@ -52,3 +53,7 @@ val try_with : (unit -> 'a) -> 'a option (** Make an option of a value *) val some : 'a -> 'a option + +(** [pp ~default data_pp ppf x] pretty-print value [x] using [data_pp] + or [default] ([""] by default) string if there is no value. *) +val pp: ?default:string ->(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a option -> unit