【问题标题】:clarify use of ns in packages阐明 ns 在包中的使用
【发布时间】:2016-04-03 12:13:05
【问题描述】:

昨天开始玩 Clojure。 我无法理解模块系统的工作原理:

  1. 我已经安装了草书
  2. 我按照 leiningen 模板创建了一个项目
  3. /src/clojure_first_steps 下有两个 clojure 文件

core.clj

(ns clojure-first-steps.core)
(:require [clojure-first-steps.utils :refer :all])

(defn run-other-foo
  (foo-2 ["hello"]))

utils.clj

(ns clojure-first-steps.utils)

(defn foo-2 [x] (x)) 

虽然 'lein compile' 运行时没有问题,但 'lein test' 无法编译 (:require [clojure-first-steps.utils :refer :all]),正在测试:

(ns clojure-first-steps.core-test
  (:require [clojure.test :refer :all]
            [clojure-first-steps.core :refer :all]))

(deftest a-test
  (testing "I can access dependecies from another module"
    (is (= "hello" (run-other-foo)))))

错误信息是java.lang.ClassNotFoundException: clojure-first-steps.utils

编辑:项目树

.
├── CHANGELOG.md
├── clojure_first_steps.iml
├── doc
│   └── intro.md
├── LICENSE
├── project.clj
├── README.md
├── resources
├── src
│   ├── clojure_first_steps
│   │   ├── core.clj
│   │   └── utils.clj
├── target
│   ├── classes
│   │   └── META-INF
│   │       └── maven
│   │           └── clojure_first_steps
│   │               └── clojure_first_steps
│   │                   └── pom.properties
│   ├── repl-port
│   └── stale
│       └── leiningen.core.classpath.extract-native-dependencies
└── test
    ├── clojure_first_steps
    │   └── core_test.clj

【问题讨论】:

  • 能否在您的项目目录中显示tree . 的输出?

标签: clojure packages require


【解决方案1】:

在你的 core.clj 中:

(ns clojure-first-steps.core) (:require [clojure-first-steps.utils :refer :all])

这是不正确的 - (:require) 子句需要在 ns 宏内。因为不是,所以查找向量中的符号(显然没有找到)。

(ns clojure-first-steps.core
  (:require [clojure-first-steps.utils :refer :all]))

这告诉 Clojure 编译器加载 clojure-first-steps.utils(如果尚未加载),并在新创建的命名空间中引用它的定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多