从Clojure调用Java

时间:2008-12-04 22:03:04

标签: java clojure language-interoperability

当我尝试在Clojure中运行以下代码(来自REPL)时:

(dotimes [i 5]
  (.start
   (Thread.
    (fn []
      (Thread/sleep (rand 1000))
      (println (format "Finished %d on %s" i (Thread/currentThread)))))))

我收到以下错误:

java.lang.Exception: Unable to resolve symbol: i in this context
clojure.lang.Compiler$CompilerException: NO_SOURCE_FILE:6: Unable to resolve symbol: i in this context
        at clojure.lang.Compiler.analyze(Compiler.java:3713)

我在这里做错了什么?

2 个答案:

答案 0 :(得分:5)

您的代码适用于我使用最新的SVN(1144)。

user> (dotimes [i 5] (.start (Thread. (fn [] (Thread/sleep (rand 1000)) (println (format "Finished %d on %s" i (Thread/currentThread)))))))

Finished 0 on Thread[Thread-16,5,main]
Finished 4 on Thread[Thread-20,5,main]
Finished 3 on Thread[Thread-19,5,main]
Finished 1 on Thread[Thread-17,5,main]
Finished 2 on Thread[Thread-18,5,main]

您使用的是9月16日的版本吗?你几乎必须使用SVO版本的Clojure。它变化太快了。

答案 1 :(得分:2)

我认为你的意思是“从Clojure调用Java”。它们仍然是fiddling with the syntax