gpt4 book ai didi

Clojure 规范错误消息 : filename? 行号? :in?的含义

转载 作者:行者123 更新时间:2023-12-02 21:03:16 25 4
gpt4 key购买 nike

很抱歉问这么基本的问题。希望答案并不明显的事实主要是由于 clojure.spec 仍然是 alpha 版本 (0.1.134)。

如何在此规范错误消息中找到违规代码的文件名和行号? :in 键 sc 的含义是什么。值[2 1]

#error {
:cause Call to clojure.core/refer-clojure did not conform to spec:
In: [2 1] val: :as fails at: [:args :exclude :op :quoted-spec :spec] predicate: #{:exclude}
In: [2 1] val: :as fails at: [:args :only :op :quoted-spec :spec] predicate: #{:only}
In: [2 1] val: :as fails at: [:args :rename :op :quoted-spec :spec] predicate: #{:rename}
In: [2] val: (quote :as) fails at: [:args :exclude :op :spec] predicate: #{:exclude}
In: [2] val: (quote :as) fails at: [:args :only :op :spec] predicate: #{:only}
In: [2] val: (quote :as) fails at: [:args :rename :op :spec] predicate: #{:rename}

:data #:clojure.spec.alpha{:problems ({:path [:args :exclude :op :spec], :pred #{:exclude}, :val (quote :as), :via [], :in [2]} {:path [:args :exclude :op :quoted-spec :spec], :pred #{:exclude}, :val :as, :via [], :in [2 1]} {:path [:args :only :op :spec], :pred #{:only}, :val (quote :as), :via [], :in [2]} {:path [:args :only :op :quoted-spec :spec], :pred #{:only}, :val :as, :via [], :in [2 1]} {:path [:args :rename :op :spec], :pred #{:rename}, :val (quote :as), :via [], :in [2]} {:path [:args :rename :op :quoted-spec :spec], :pred #{:rename}, :val :as, :via [], :in [2 1]}), :spec #object[clojure.spec.alpha$regex_spec_impl$reify__1188 0x437e951d clojure.spec.alpha$regex_spec_impl$reify__1188@437e951d], :value ((quote :exclude) (quote [reduce into merge map take partition partition-by]) (quote :as) (quote core)), :args ((quote :exclude) (quote [reduce into merge map take partition partition-by]) (quote :as) (quote core))}
...}

当我输入lein repl时出现此错误。我对有问题的行的猜测在这里:

(ns fargish.util
(:refer-clojure :exclude [rand rand-int cond])
(:require [better-cond.core :refer [cond]]
[clojure.tools.trace :refer :all]
[clojure.pprint :refer [pprint]]
[clojure.math.numeric-tower :as math]
[clojure.core.async :as async :refer [<! <!! >! >!!]]
[clojure.java.io :as io]
[clojure.edn :as edn]
[popen :refer [popen] :as po]
[cheshire.core :as ch]
[lonocloud.synthread :as ->]))

因为当我从 Clojure 1.8 下运行的目录复制此错误消息时,第一次出现了错误消息。此代码在 Clojure 1.8 下运行。

如果我没有有这个方便的线索,我想知道如何追踪有问题的文件和行。即使有了这条线索,我也不知道该怎么办。我无法判断错误是在我的代码中、在 clojure.core/ns 代码中还是在 clojure.core/ns 中的规范中。另外,In: [2 1]指的是什么?

<小时/>

更新我发现了错误。我上面的猜测是错误的。该错误甚至不在我的代码中!它位于 clojure.core.async 中。我使用的是旧版本:0.2.374。我更改为当前版本 0.3.443,错误消息就此结束。显然,Clojure 1.9 之前编写的许多代码都利用了 ns 宏中的一些松懈,例如在 :require 和其他关键字之前省略了冒号,因此这将是常见问题,直到库更新并且依赖项更新以指向更新的库。类似的错误出现在popen中(已经修复了——服务很快!)。

不过,我的问题不是关于此错误的具体情况。这是关于一般问题:如何找到有问题的代码行,而不需要像这样通过反复试验进行长时间的追踪。

最佳答案

当您在 ns 形式中犯错误时,您收到的错误消息会非常糟糕(比“平均错误”Clojure 错误消息更糟糕)。我发现的唯一解决方案是在测试运行之间保持非常有限的更改数量。

我使用的另一种技术是在 Google 中搜索可以复制的正确代码示例。我今天使用该技术来找到正确的语法:

(ns xyz
(:require
[clojure.spec.alpha :as sp]
[clojure.spec.gen.alpha :as gen]
[clojure.spec.test.alpha :as stest] ))

(stest/check `i/truthy? {:clojure.spec.test.check/opts {:num-tests 99}})

请注意,与 require 语句不同,关键字命名空间中没有“alpha”,这进一步造成了困惑。我敢让你尝试猜出正确答案from the online docs :

Usage: (check) (check sym-or-syms) (check sym-or-syms opts) Run generative tests for spec conformance on vars named by sym-or-syms, a symbol or collection of symbols. If sym-or-syms is not specified, check all checkable vars.

The opts map includes the following optional keys, where stc aliases clojure.spec.test.check:

::stc/opts opts to flow through test.check/quick-check :gen
map from spec names to generator overrides

The ::stc/opts include :num-tests in addition to the keys documented by test.check. Generator overrides are passed to spec/gen when generating function args.

如果所有其他方法都失败了,你别无选择,只能使用历史悠久的技术,找到一个确实有效的 super 最小案例,然后一次为你的目标添加一个小的改变,直到实现失败。我经常发现在一个新的、干净的 lein 项目中进行此类实验更容易,因此我确信不存在混淆该问题的未知问题。

<小时/>

附注你可以随时询问别人。选择以下一项或多项:

关于Clojure 规范错误消息 : filename? 行号? :in?的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46615500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com