In Clojure what is the idiomatic way to test for nil and if something is nil then to substitute a value?
For example I do this a lot:
let [ val (if input-argument input-argument "use default argument")]
: but I find it repetitive having to use "input-argument" twice.
Alex's suggestion of "or" is indeed the idiomatic way to rewrite your example code, but note that it will not only replace nil
values, but also those which are false
.
If you want to keep the value false
but discard nil
, you need:
(let [val (if (nil? input-argument) "use default argument" input-argument)]
...)