为什么Clojure编译器没有为错误的类型提示抛出错误?

时间:2016-01-23 12:18:49

标签: clojure compiler-errors compiler-optimization type-hinting

假设:

  • 我得到的类型提示是关于性能优化,而不是类型检查。我想在性能优化无效的情况下解决问题。

假设我有以下代码:

example.com/index.php?page=search/web&search=[url]&type=Web&fl=0

当我跑步时,我得到:

(ns test.core)

(defrecord SquarePeg [width length])
(defrecord RoundHole [radius])

(def square-peg (SquarePeg. 5 50))

(defn insert-peg [^test.core.RoundHole peg]
  (println "insert-peg inserted with: " peg))

(defn -main
  "Insert a square peg in a round hole"
  [& args]
  (insert-peg square-peg))

现在我希望这有一些类型提示类型提示错误,但事实并非如此。

现在我正在查看Clojure Compiler code - 我看到以下提示处理代码:

但是我没有看到它处理类型提示失败的位置。

我的问题是:为什么Clojure编译器不会因错误的类型提示而抛出错误?

1 个答案:

答案 0 :(得分:4)

大多数类型提示[1]只影响否则会使用反射的代码 - 即互操作代码。

由于您的insert-peg函数没有执行任何互操作,因此类型提示没有用处,并且它被忽略。

当你的类型提示导致clojure编译器为调用一种类型的方法编写字节码时会发生类型错误,但在运行时,实例结果是另一种类型。

[1]见Alex的评论中的例外情况