Why doesn't the Clojure compiler throw an error for the wrong type of hint?

Assumptions:

  • I get that type of hints is performance optimization, not type checking. I try to work when performance optimization is ineffective.

Suppose I have the following code:

(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))

When I run it, I get:

insert-peg inserted with:  #direct_linking_test.core.SquarePeg{:width 5, :length 50}

Now I expect this to have some indication that the type hint was wrong, but it is not.

Now I will look at the Clojure compiler code - and I see the following hint processing code:

But I don't see a bit where it handles a hint type failure.

: Clojure ?

+4
1

[1] , - .. .

insert-peg , , .

, clojure - , .

[1] . Alex

+4

Source: https://habr.com/ru/post/1625624/


All Articles