Clojure deftype 调用函数在同一个命名空间中抛出 "java.lang.IllegalStateException: Attempting to call unbound fn:"

标签 clojure annotations interop

我将 Clojure 放入一个大量使用 Jersey 和 Annotations 的现有 Java 项目中。我希望能够利用以前工作的现有自定义注释、过滤器等。到目前为止,我一直在粗略地使用 deftype 方法和 javax.ws.rs 注释在 Clojure Programming 的第 9 章中找到。 .

(ns my.namespace.TestResource
  (:use [clojure.data.json :only (json-str)])
  (:import [javax.ws.rs DefaultValue QueryParam Path Produces GET]
           [javax.ws.rs.core Response]))

;;My function that I'd like to call from the resource.
(defn get-response [to]
  (.build
    (Response/ok
      (json-str {:hello to}))))

(definterface Test
  (getTest [^String to]))

(deftype ^{Path "/test"} TestResource [] Test
  (^{GET true
     Produces ["application/json"]}
  getTest
  [this ^{DefaultValue "" QueryParam "to"} to]
  ;Drop out of "interop" code as soon as possible
  (get-response to)))

正如您从评论中看到的,我想在 deftype 之外调用函数,但在同一个命名空间内。至少在我看来,这让我可以将 deftype 重点放在互操作和连接到 Jersey 上,并将应用程序逻辑分开(更像是我想编写的 Clojure)。

但是,当我这样做时,我得到以下异常:
java.lang.IllegalStateException: Attempting to call unbound fn: #'my.namespace.TestResource/get-response

deftype 和命名空间有什么独特之处吗?

最佳答案

......有趣的是,我在这个问题上的时间并没有产生答案,直到我在这里问:)

看起来命名空间加载和 deftypes 已在 this post. 中得到解决。我怀疑 deftype 不会自动加载命名空间。正如在帖子中发现的,我能够通过添加这样的要求来解决这个问题:

(deftype ^{Path "/test"} TestResource [] Test
  (^{GET true
     Produces ["application/json"]}
    getTest
    [this ^{DefaultValue "" QueryParam "to"} to]
    ;Drop out of "interop" code as soon as possible
    (require 'my.namespace.TestResource) 
    (get-response to)))

关于Clojure deftype 调用函数在同一个命名空间中抛出 "java.lang.IllegalStateException: Attempting to call unbound fn:",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10953621/

相关文章:

clojure - Clojure 类重载是如何工作的?

java - 用注释标记数组的不同维度

c# - 将简单的 C# DLL 转换为 COM 互操作组件

c# - 从 C# 设置非托管 C dll 的公共(public)字段

clojure - 使用 core.async 进行节流功能

vector - 更新嵌套向量

clojure - 作为映射操作的结果,如何仅返回真值

annotations - 如何在 Julia Plots 中制作 3D 注释

java - 使用 Intellij 和 Maven 调试 Java 注释处理器

c# - 在外部应用程序中设置文本框的文本。 Win32 API