【发布时间】:2013-07-31 16:22:21
【问题描述】:
我跑了lein new app hm,然后在hm/src/hm 编辑core.clj 成为:
(ns hm.core
(:gen-class)
(:use [hm.hashmap]))
(defn -main []
(def j (new hm.hashmap))
(-add j "foo" "bar")
(println j))
和hashmap.clj 是:
(ns hm.hashmap
(:gen-class
:methods [[hashmap [] java.util.HashMap]
[add [String String]]]))
(defn -hashmap []
(def h (new java.util.HashMap))
h)
(defn -add [this key value]
(. this put key value)
this)
目标是围绕 HashMap 做一个包装器,这样我就可以理解 Clojure 以及它与 Java 的关系。我对 Clojure 还很陌生。但是,当我编译这个时,我在hashmap.clj 中得到了很多 ClassNotFoundException。我怎样才能做到这一点?
【问题讨论】:
-
找不到什么类?
-
@JeremyHeiler
Exception in thread "main" java.lang.ClassNotFoundException: java.lang., compiling:(hm/hashmap.clj:1:1) -
这不是在 Clojure 中做事的正常方式,我会将其与从内联汇编块开始学习 C 进行比较——而 clojure 对象成为 jvm 中的类和方法,根据类工作Clojure 中的方法和方法(除了您要引入 java 类的互操作)是一种罕见且专门的东西。我不会从生成 java 类开始,而是从使用 java 类的 Clojure 函数开始,然后从那里开始构建。
-
@noisesmith 感谢您的建议!我可以从哪里开始?
-
4clojure.com 和 github.com/functional-koans/clojure-koans 是对 clojure 的很好的交互式介绍。 O'Reilly Clojure Programming 一书(Chas Emerick 撰写)和 The Joy of Clojure 也是很好的资源,其中包含有关 Java 互操作的全面信息。您在这里尝试的那种互操作几乎只有在您编写希望能够从中调用 Clojure 代码的 Java 代码时才需要 - 反过来更容易。