I want to supply a default value which can be overridden. I know I can use a ternary, like this:
(def foo (if (not (nil? bar)) bar baz))
But surely there is a more idiomatic way in Clojure to say “use bar, or baz if bar is nil.
Any suggestions?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This will assign
barunless it isnilorfalse, andbazotherwise.EDIT
If you wish to check for
nilprecisely, you can slightly optimize your original code, like this:I believe, this is the shortest possible way, though not idiomatic.