gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:56:37 24 4
gpt4 key购买 nike

我将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com