Should the clojure "-main" function have a return value?

I’m wondering if the Clojure “-main” function is supposed to have a return value, like the C “main” function? From googling around a bit, I think the answer is “no”, but I am not certain. For now, I am just using a -main function structure like this, to handle passing the status code back to the OS:

(defn -main
  "the main function"
  [& args]

  (let [status-code (apply main-with-return-status args)]
    (if (> status-code 0)
      (let [status-message (str "Status code is: " status-code)]
        (if (not (is-in-repl))
          (do
            (println->stderr status-message)
            (System/exit status-code))
          (throw (Exception. status-message)))
        )
      )
    )
  )

or, if the code is running in a REPL, I just throw an exception if the final status-code is nonzero. Is that on the right track?

It’s the equivalent of Java’s public static void main( String[] args ) method so, no, it won’t return anything.

1 Like

Thanks, @seancorfield

It can’t have a return value. The process itself will have an exit code though, which by default is 0 (success) and will usually be 1 if the process exits due to an uncaught exception in the main thread. Or you can explicitly set it like (System/exit 42).

2 Likes

Thank you @alexmiller, this is helpful.

Your book Programming Clojure has been very helpful as I make my way in this new (for me) world. :+1: