Clojure deftype 在同一个命名空间中调用函数抛出“java.lang.IllegalStateException: Attempting to call unbound fn:" [英] Clojure deftype calling function in the same namespace throws "java.lang.IllegalStateException: Attempting to call unbound fn:"

查看:22
本文介绍了Clojure deftype 在同一个命名空间中调用函数抛出“java.lang.IllegalStateException: Attempting to call unbound fn:"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Clojure 放入一个大量使用 Jersey 和 Annotations 的现有 Java 项目中.我希望能够利用以前工作的现有自定义注释、过滤器等.到目前为止,我已经粗略地使用了在 Clojure Programming.

I'm placing Clojure into an existing Java project which heavily uses Jersey and Annotations. I'd like to be able to leverage the existing custom annotations, filters, etc of the previous work. So far I've been roughly using the deftype approach with javax.ws.rs annotations found in Chapter 9 of Clojure Programming.

(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).

As you can see from the comments, I'd like to call functions outside the deftype, but within the same namespace. At least in my mind, this allows me to have the deftype focus on interop and wiring up to Jersey, and the application logic to be separate (and more like the Clojure I want to write).

但是,当我这样做时,我得到以下异常:

However when I do this I get the following exception:

java.lang.IllegalStateException: Attempting to call unbound fn: #'my.namespace.TestResource/get-response

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

Is there something unique about a deftype and namespaces?

推荐答案

... 有趣的是,我在这个问题上的几个小时没有得到答案,直到我在这里提问:)

... funny my hours on this problem did not yield an answer until after I asked here :)

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

It looks like namespace loading and deftypes was addressed in this post. As I suspected the deftype does not automatically load the namespace. As found in the post, I was able to fix this by adding a require like this:

(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:"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆