【问题标题】:core.typed for nested Vecs嵌套 Vecs 的 core.typed
【发布时间】:2015-04-11 03:10:21
【问题描述】:

我正在尝试为此数据结构定义类型定义:

[[:id :int :null false :pk true :autoinc true]
[:fooname [:varchar 40] :null false]
[:age :int :null false]]

然后我有一个过滤器函数,它删除每个包含:id 作为第一个元素的向量,如下所示:

(defn filter-id-columns [l]
  (remove #(= :id (first %)) l))

但是,无论我尝试什么,我总是会收到这样的类型错误:

Type Error (leiningen/code_generator.clj:23:19) Polymorphic function first could not be applied to arguments:
Polymorphic Variables:
x

Domains:
(t/HSequential [x t/Any *])
(t/Option (t/EmptySeqable x))
(t/NonEmptySeqable x)
(t/Option (clojure.lang.Seqable x))

Arguments:
t/Any

Ranges:
x :object {:path [(Nth 0)], :id 0}
nil
x
(t/Option x)

in: (first p1__27917#)
in: (first p1__27917#)

我知道我为输入参数提供了错误的类型,但是我找不到正确的东西。

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: clojure nested clojure-core.typed


    【解决方案1】:

    函数参数默认为Any。您必须像这样扩展您的本地函数定义:(t/fn [x :- (U nil (Seqable Any))] ...)

    【讨论】:

    • 谢谢,虽然我不知道这很有意义。这是否意味着我不能在没有类型声明的情况下使用匿名函数,从而使每个函数都“显式”?
    • 您可以使用 ann-form 提供预期类型:(t/ann-form #(...) [(U nil (Seqable Any) -> ...])
    • 谢谢。使用 core.typed fn 函数而不是 clojure fn 函数也很有意义。很高兴在这里有选择。
    猜你喜欢
    • 2019-03-17
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多