【发布时间】:2016-07-22 14:28:49
【问题描述】:
当我在 Scala 中工作时,我喜欢我可以对类型进行模式匹配,并且类型检查器会使用该类型:
val x : Any = "boop"
x match {
case y : String => do-something-stringy(y);
case z : Int => .... etc
}
我知道在 core.typed 中,条件将帮助类型检查器解析确切的类型。我尝试使用 core.match 复制它:
(ann do-something-stringy [String -> String])
(defn do-something-stringy [message]
(str "Hello " message))
;; Doesn't work
(ann do-things [Object -> String])
(defn do-things [foo]
(match [(type foo)]
[String] (do-something-stringy foo)
:else "Nope"))
这会失败并出现错误:
函数 do-something-stringy 无法应用于参数:
领域: 字符串
参数: 对象
范围: 字符串
具有预期类型: 字符串
有没有办法使用 core.match 让它工作?
谢谢!
【问题讨论】:
标签: clojure clojure-core.typed