如果你从一个命名空间中引用一个变量,然后你可以为该变量def一个新值,这将对另一个命名空间进行修改,并按照下面示例中的错误一样出错。
顺便说一句,Clojure不允许这样做,拒绝评估涉及引用变量的def,并发出类似于下面的错误诊断
CompilerException java.lang.IllegalStateException: foo已经引用了:#'some.name.space/foo在命名空间:user,编译:(NO_SOURCE_PATH:2:1)
以下是一个描述问题的完整示例
给定
`
(ns foo.core)
(defn square [x]
(* x x))
`
然后在REPL中这样做
`
cljs.user=> (require '[foo.core :refer [square]])
nil
cljs.user=> (var square)
'foo.core/square
cljs.user=> (square 3)
9
cljs.user=> (ns-interns 'cljs.user)
{}
cljs.user=> (defn square [x] (+ x x))
WARNING: square已经引用了:foo.core/square正在被:cljs.user/square替换,在行1
'foo.core/square
cljs.user=> (square 3)
6
cljs.user=> (var square)
'foo.core/square
cljs.user=> (in-ns 'foo.core)
nil
foo.core=> (square 3)
6
foo.core=> (in-ns 'cljs.user)
nil
cljs.user=> (ns-interns 'cljs.user)
{square #'cljs.user/square}
cljs.user=> (cljs.user/square 3)
TypeError: Cannot read property 'call' of undefined
at repl:1:105
at repl:9:3
at repl:14:4
at Object.exports.runInThisContext (vm.js:54:17)
at Domain.<anonymous> ([stdin]:41:34)
at Domain.run (domain.js:221:14)
at Socket.<anonymous> ([stdin]:40:25)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
cljs.user=> #'cljs.user/square
'cljs.user/square
cljs.user=> @#'cljs.user/square
nil
`