【发布时间】:2014-08-03 18:45:41
【问题描述】:
我正在我的项目中实现core.typed 注释。以下代码工作正常:
(require ['clojure.core.typed :as 't])
(t/ann foo [String String -> String])
(defn foo [x y]
(str x y))
(t/ann works [String String -> String])
(defn works [x y]
(t/let [z :- (t/Vec String), (shuffle [x y])
a :- String, (nth z 0)
b :- String, (nth z 1)]
(foo a b)))
如果我稍微改变函数定义,我会得到几个不同的错误:
(t/ann fails-1 [String String -> String])
(defn fails-1 [x y]
(t/let [z :- (t/Vec String), (shuffle [x y])
; Fails because (t/Vec String) is not included
; in the domain of first or second
a :- String, (first z)
b :- String, (second z)]
(foo a b)))
(t/ann fails-2 [String String -> String])
(defn fails-2 [x y]
(t/let [[a b] :- (t/Vec String), (shuffle [x y])]
; Fails because a and b are of type (t/U nil String)
(foo a b)))
从类型的角度来看,我预计这 3 个示例或多或少是等价的。具体来说,我假设:
-
(t/Vec String)将在nth和first的域中 - 当
x和y的类型为String时,(shuffle [x y])的范围将不包括nil
这是 core.typed 的限制,还是我有根本的误解?
【问题讨论】:
标签: clojure clojure-core.typed