From 0e2aa97f3aa1341693885511fd78394b828c6477 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Thu, 26 Jul 2018 20:45:55 +0200 Subject: lib/trivial: add assertMsg --- lib/trivial.nix | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'lib/trivial.nix') diff --git a/lib/trivial.nix b/lib/trivial.nix index e702b8cdcc9f..bba284548d98 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -188,6 +188,26 @@ rec { warn = msg: builtins.trace "WARNING: ${msg}"; info = msg: builtins.trace "INFO: ${msg}"; + /* Print a trace message if pred is false. + Intended to be used to augment asserts with helpful error messages. + + Example: + assertMsg false "nope" + => false + stderr> trace: nope + + assert (assertMsg ("foo" == "bar") "foo is not bar, silly"); "" + stderr> trace: foo is not bar, silly + stderr> assert failed at … + + Type: + assertMsg :: Bool -> String -> Bool + */ + assertMsg = pred: msg: + if pred + then true + else builtins.trace msg false; + ## Function annotations -- cgit v1.2.3