如何在clojure中使用自己的命名空间之外的类型? [英] How do you use a type outside of its own namespace in clojure?

查看:127
本文介绍了如何在clojure中使用自己的命名空间之外的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目设置与leiningen称为techne。我创建了一个名为scrub的模块,名为Scrub的类型和一个名为foo的函数。

I have a project set up with leiningen called techne. I created a module called scrub with a type in it called Scrub and a function called foo.

techne / scrub.clj:

techne/scrub.clj:

(ns techne.scrub)
  (deftype Scrub [state]
    Object
     (toString [this]
     (str "SCRUB: " state)))

(defn foo
  [item]
  (Scrub. "foo")
  "bar")

techne / scrub_test.clj:

techne/scrub_test.clj:

(ns techne.scrub-test                                                                                                                                             
  (:use [techne.scrub] :reload-all)                                                                                                                               
  (:use [clojure.test]))                                                                                                                                          


(deftest test-foo                                                                                                                                                 
  (is (= "bar" (foo "foo"))))                                                                                                                                                           

(deftest test-scrub                                                                                                                                               
  (is (= (Scrub. :a) (Scrub. :a)))) 

当我运行测试时,错误:

When I run the test, I get the error:

Exception in thread "main" java.lang.IllegalArgumentException: Unable to resolve    classname: Scrub (scrub_test.clj:11)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:5376)
    at clojure.lang.Compiler.analyze(Compiler.java:5190)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:5357)

如果我删除test-scrub一切正常。为什么:使用techne.scrub'import'函数定义而不是类型定义?如何引用类型定义?

If I remove test-scrub everything works fine. Why does :use techne.scrub 'import' the function definitions but not the type definitions? How do I reference the type definitions?

推荐答案

因为 deftype 生成一个类,您可能需要在您的ns定义中使用(:import [techne.scrub Scrub])在techne.scrub-test中导入该Java类。

Because deftype generates a class, you will probably need to import that Java class in techne.scrub-test with (:import [techne.scrub Scrub]) in your ns definition.

我实际上在这里写下了关于defrecord的同样的事情:

I actually wrote up this same thing with respect to defrecord here:

  • http://tech.puredanger.com/2010/06/30/using-records-from-a-different-namespace-in-clojure/

你可以做的另一件事是在scrub中定义一个构造函数:

Another thing you could do would be to define a constructor function in scrub:

(defn new-scrub [state] 
  (Scrub. state))

,然后您不需要导入Scrub测试擦洗。

and then you would not need to import Scrub in test-scrub.

这篇关于如何在clojure中使用自己的命名空间之外的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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