【发布时间】:2016-02-22 15:00:42
【问题描述】:
刚刚进入 Clojure,我在命名空间、路径和类语法方面遇到了一些问题。
我首先测试并编写了一个简单的 hello world 函数。
test/test_subject_test.clj:
(ns test-subject-test
(:require [clojure.test :refer :all]
[test-subject :refer :all]))
(deftest testit
(testing "All good?"
(is (= "I got this" (subject))))
src/test_subject.clj:
(ns test-subject)
(defn subject []
"I got this")
一切正常,所以我决定接下来尝试在类上调用静态方法。
test/test_subject_test.clj:
(ns test-subject-test
(:require [clojure.test :refer :all]
[test-subject :refer :all]
[hello :refer :all]))
(deftest testit
(testing "All good?"
(is (= "I got this" (subject))))
(testing "Try calling a static method"
(is (= "Hello Guy!" (-handler Hello "Guy")))))
src/hello.clj:
(ns hello
(:gen-class :name "Hello"
:methods [^:static [handler [String] String]]))
(defn -handler [s]
(str "Hello " s "!"))
但是现在编译器会抛出一个很长的堆栈跟踪,其中包含一条有趣的消息:Caused by: java.lang.RuntimeException: Unable to resolve symbol: Hello in this context。我尝试了:require 语句和(-handler Hello) 调用的一些排列,我尝试不使用:refer :all 并将该方法称为(-handler (.Hello hello)),但显然我错过了一些关于如何类的非常基本的东西在 Clojure 中工作。
我查看了Clojure vars and Java static methods,但它似乎比我想要做的事情复杂得多:只需调用一个静态方法。然而,问题中有一个有趣的引述,这让我尝试了这个:(Hello handler "Guy") without success (Unable to resolve symbol: Hello in this context)。
那么,我应该如何从另一个文件中获取一个类并在其上调用一个静态方法?
【问题讨论】:
标签: clojure