Executing multiple statements in if-else without nullpointer exception

12,984

Use do:

(defn status []
  (if server 
    (do
      (println "server is up and running")
      (println "connections:" (connection-count server)))    
    (println "server is down")))

In Lisps, generally, you can't just add parens for grouping.

((println "foo") (println "foo"))

Here, the return value of the first (println "foo") will be tried to be called (as a function), with the return value of the second as an argument. Those are very basic evaluation rules, so I suggest that you hit some introductory books or documentation about Clojure or Lisps in general.

From the evaluation section of the Clojure homepage:

Non-empty Lists are considered calls to either special forms, macros, or functions. A call has the form (operator operands*).

Macros or special forms may "break" this rule.

Share:
12,984

Related videos on Youtube

echox
Author by

echox

Updated on June 16, 2020

Comments

  • echox
    echox almost 4 years

    I'm trying to dig a little deeper into clojure and functional programming.

    At some point of my code I have a (def server (spawn-server)). Now I want a short function for the REPL to check the state of this socket.

    This is what I have at the moment:

    (defn status []
      (if server 
        (
          (println "server is up and running")
          (println "connections:" (connection-count server)) 
         )    
        (println "server is down")))
    

    If server is nil, everything works fine, but this is the output on the REPL if the server is running:

    => (status)
    server is up and running
    connections: 0
    #<CompilerException java.lang.NullPointerException (NO_SOURCE_FILE:0)>
    

    I'm not really sure if I see the problem, but I can't figure out how this should work :-) What I have here is this:

    ((println "foo")(println "foo"))
    

    which will be evaluated to (nil nil) which results in the NullPointerException ?

    Usally I wouldn't use the outer parentheses but how can I create some kind of "block" statement for the if-condition. If I don't use them, the second println will be used as else.

    What would work is the usage of let as some kind of "block"-statement:

    (let [] 
      (println "server is up and running"),
      (println "connections:" (connection-count server)) )
    

    But I'm not really sure if this the "right" solution?

  • echox
    echox almost 13 years
    Thanks, that was what I searched for.
  • matanster
    matanster over 7 years
    Why the null pointer exception after all these years? this is so unfriendly ;-)
  • danlei
    danlei over 7 years
    @matanster I don't know much about the reasons, but I agree that Clojure's error messages aren't the best.
  • ianjs
    ianjs over 6 years
    A bit late to the party, but to specifically explain the null pointer exception, it is because the first println returns nil. The parenthesis after the if says "evaluate the first element in this list as a function". Because the first element evaluates to nil it throws the exception. Not that obvious, but a null pointer exception in Clojure is often becuase you tried to execute nil as function.
  • danlei
    danlei over 6 years
    @ianjs Yes, that's correct, and the answer basically says the same. ("[T]he return value of the first (println "foo") will be tried to be called (as a function), with the return value of the second as an argument".) If you're answering to @matanster 's comment, however, I think his point was that the error message is not very intuitive – especially for beginners. CCL, for example, says: Error: Car of ((PRINC "foo") (PRINC "foo")) is not a function name or lambda-expression. Much better, isn't it? Clojure's error message feels rather low-level for such a high-level language, IMO.