无法解决clojure.string/replace";不是一个函数;错误
以下是ClojureScript的字符串“replace”函数的原始代码块:无法解决clojure.string/replace";不是一个函数;错误,replace,clojurescript,Replace,Clojurescript,以下是ClojureScript的字符串“replace”函数的原始代码块: (defn replace "Replaces all instance of match with replacement in s. match/replacement can be: string / string pattern / (string or function of match)." [s match replacement] (cond (string? match)
(defn replace
"Replaces all instance of match with replacement in s.
match/replacement can be:
string / string
pattern / (string or function of match)."
[s match replacement]
(cond
(string? match)
(.replace s (js/RegExp. (gstring/regExpEscape match) "g") replacement)
(instance? js/RegExp match)
(if (string? replacement)
(replace-all s match replacement)
(replace-all s match (replace-with replacement)))
:else (throw (str "Invalid match arg: " match))))
正如您在这一行中看到的:[s match replacement]
,此方法接受三个参数
从我的回复:
user=> (replace ":c41120" ":" "")
ArityException Wrong number of args (3) passed to: core/replace clojure.lang.AFn.throwArity (AFn.java:429)
我是唯一一个认为我通过了正确数量的论点(3)的人吗?知道为什么会失败吗
问题,第二部分:具体化
在我的components.cljs文件中,我有以下“requires”:
(ns labrador.components
(:require [re-frame.core :as rf]
[reagent.core :refer [atom]]
[clojure.string :as s]
[labrador.helpers :as h]))
我在此文件中成功地使用了“s/join”和“s/blank”。但是当我尝试使用下面的“s/replace”时(请注意,“replace”调用位于第484行):
…我得到以下错误:
Uncaught TypeError: s.replace is not a function
at clojure$string$replace (string.cljs?rel=1489020198332:48)
at components.cljs?rel=1489505254528:484
…当我显式调用replace函数时,如下所示:
code (clojure.string/replace key ":" "")]
…我仍然会收到完全相同的错误,就好像我仍然在调用“s/replace”
我不熟悉Clojure/ClojureScript,对自己的无知一无所知 首先,看起来您运行的是Clojure REPL,而不是ClojureScript,其次,您调用的是,而不是。我发现了错误。我试图替换一个键,而不是一个字符串。调用replace函数之前,我将键转换为字符串,通过将
(s/replace键):“”
更改为(s/replace(str键):“”
,一切都很好
这个模棱两可的错误消息使我偏离了方向。被告知函数“replace”显然不是函数,而不是被告知函数无法执行其任务,因为传递的数据不是字符串,这只需要花费我大约三个小时的开发时间。当第一个参数是数字而不是字符串时,我就遇到了这个问题
我认为错误消息有点误导性是的,clojure.string/replace在我的REPL中起作用,我已经在ClojureScript中成功地使用了它。谢谢你澄清这一区别。但当我尝试这个特殊的替换函数时,会出现错误。我将用更多细节更新我的问题。如果您试图从关键字中获取不带前导:,则可以使用
name
函数。为了澄清,错误并不表示clojure.string/replace
不是函数。相反,它是实现中调用(.replace s…
失败的结果。将(让[s:abc](.replace s:'')与(让[s:abc](.replace s:'')进行比较)
。后者再现了错误,本质上表明没有在关键字上定义replace
函数。谢谢!解决了我的案子
code (clojure.string/replace key ":" "")]