【问题标题】:Can core.match be used to add type information for an object?可以使用 core.match 为对象添加类型信息吗?
【发布时间】: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


    【解决方案1】:

    尝试在class 而不是type 上调度。 core.typed 对 type 没有真正的支持,因为它在元数据方面很奇怪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-21
      • 2021-09-13
      • 2023-01-14
      • 1970-01-01
      • 2017-02-25
      • 2023-03-21
      • 2022-12-05
      • 1970-01-01
      相关资源
      最近更新 更多