Kevin Burke
Kevin Burke

Reputation: 64844

Run two commands in a row after an if statement in Clojure

Why does the following Clojure program throw a NullPointerException?

user=> (defn x []  
       "Do two things if the expression is true."
       (if true ((println "first expr") (println "second expr")) false))

user=> (x)
first expr
java.lang.NullPointerException (NO_SOURCE_FILE:0)
second expr

This is a simplified version of my actual use case, where I want to execute maybe three statements (pull values from the DB) before returning a map - {:status 200, :body "Hello World"} inside of the branch.

Upvotes: 19

Views: 7842

Answers (3)

bmillare
bmillare

Reputation: 4233

Not that it matters in your particular case, but do know the difference between (do ...) which will load each form in its own classloader, and an empty let form (let [] ...) which evaluates the whole form in a single classloader.

Upvotes: 7

Random832
Random832

Reputation: 39000

It is trying to treat the result of the first println as a function to call on the second println function.

You need a do.

(defn x []  
   "Do two things if the expression is true."
   (if true (do (println "first expr") (println "second expr")) false))

(x)

The do special form (progn in CL, begin in Scheme) executes each of its arguments in sequence and returns the result of the last one.

Upvotes: 36

Dave Ray
Dave Ray

Reputation: 40005

If nil is ok as a return value in the else case, consider using when which has an implicit do block:

(defn x []  
  "Do two things if the expression is true."
  (when true
    (println "first expr") 
    (println "second expr")))

Upvotes: 11

Related Questions