Вопрос

I'm trying to use Clojure to run my Leiningen project. Even though LightTable says it's connected in the connections pane, it won't execute unless I call the main function manually.

project.clj:

(defproject lein-test "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.5.1"]]
  :main lein-test.core)

core.clj:

(ns lein-test.core)

(defn -main [& args]
  (println "asdf"))

When I eval the entire file with ctrl+shift+enter, I see nil by the ns and nothing in the console. If I add

(-main)

then the console shows 'asdf'. lein run in the command prompt yields the expected behavior. What am I missing?

Это было полезно?

Решение

Control+Shift+Enter evaluates the namespace.

Evaluating a namespace should not run any of its functions, unless you call them at the top level. In a given codebase there should ideally be only one function that is called at the top level (conventionally, the -main function), and one should set this up not by calling it in the namespace code, but as you have, via configuration.

Everything is working as expected here. You can put a call to (-main) in a commented block or a temporary section of the file for convenience while developing, or call it directly from the repl interface.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top