Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
clojure:redefs不';不能使用clojure.core函数吗?_Clojure - Fatal编程技术网

clojure:redefs不';不能使用clojure.core函数吗?

clojure:redefs不';不能使用clojure.core函数吗?,clojure,Clojure,我有一个关于redefs的问题。下面的示例没有按预期工作。 在findmax中,始终调用clojure.core/max,而不是 带有redefs的语句 (defn findmax [x y] (max x y)) (with-redefs (clojure.core/max (fn [x y] (- x y))) (findmax 2 5)) 当我进行以下更改时,一切正常: (defn mymax [x y] (max x y)) (defn findmax [x y]

我有一个关于redefs
的问题。下面的示例没有按预期工作。
在
findmax
中,始终调用
clojure.core/max
,而不是 带有redefs的
语句

(defn findmax [x y]
  (max x y))

(with-redefs (clojure.core/max (fn [x y] (- x y)))
  (findmax 2 5))
当我进行以下更改时,一切正常:

(defn mymax [x y]
  (max x y))

(defn findmax [x y]
  (mymax x y))

(with-redefs (my/max (fn [x y] (- x y)))
  (findmax 2 5))

这里我做错了什么?

max
是Clojure编译器为大于1的算术数内联的,因此编译代码中没有对Var
#'Clojure.core/max
的引用,也无法通过更改其根绑定来更改使用
#max
的代码片段的行为。对于arity 1,这种情况不会发生:

(defn my-max [& args] :foo)

(with-redefs [clojure.core/max my-max] (max 0))
;= :foo
(with-redefs [clojure.core/max my-max] (max 0 1))
;= 1
(with-redefs [clojure.core/max my-max] (max 0 1 2))
;= 2

;; and so on
这由
max
源代码中键
:inline
:inline arities
处的条目控制;请参见
(源代码最大值)

clojure.core
中有很多自动内联函数——大部分是简单的算术运算。客户机代码可以自由定义新的变量(通过附加显式
:内联
和可能的
:内联算术
元数据或使用
定义行
)。预期的效果与定义宏类似,只是内联函数仍可用于更高阶的使用。需要注意的是,当前的实现有其令人惊讶的地方(例如,请参见Clojure JIRA以及与之相关的最新问题),因此在一天结束时,对于客户机代码,仔细使用常规宏和配套函数可能是目前的首选。将来,内联函数很可能会被与编译器宏配对的常规函数所取代——这就是ClojureScript模型