NoIdeaHowToFixThis
NoIdeaHowToFixThis

Reputation: 4564

clarify use of ns in packages

Started playing with Clojure yesterday. I can't get around how the module system works:

  1. I have installed cursive
  2. I have created a project following the leiningen template
  3. I have two clojure files under /src/clojure_first_steps

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

although 'lein compile' runs without probs, 'lein test' fails to compile on (:require [clojure-first-steps.utils :refer :all]), the test being:

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

The error message is java.lang.ClassNotFoundException: clojure-first-steps.utils

EDIT: Project tree

.
├── 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

Upvotes: 0

Views: 44

Answers (1)

noisesmith
noisesmith

Reputation: 20194

In your core.clj:

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

This is incorrect - the (:require) clause needs to be inside the ns macro. Because it is not, the symbols in the vector are looked up (and obviously not found).

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

This tells the Clojure compiler to load clojure-first-steps.utils (if it has not already), and refer it's definitions in your newly created namespace.

Upvotes: 2

Related Questions