Strange syntax for associating type with variable for occurrence typing

97 views Asked by At

In https://github.com/clojure/core.typed/wiki/User-Guide#occurrence-typing an example of occurrence typing is provided:

clojure.core.typed=> (cf (let [a (ann-form 1 Any)]
                           (cond
                            (symbol? a) a
                            (number? a) a)))
(U Sym Num nil)

Why exactly is (ann-form 1 Any) used? I guess it is a hacky/idiomatic way to set the type of a by annotating 1 and then setting a to that value, but I can't really understand why this is chosen syntax for doing this.

1

There are 1 answers

3
Ambrose On BEST ANSWER

ann-form just forgets type information about a particular expression.

Here we forget 1 is a (Val 1), and consider it of type Any for the rest of the program. This way we can demonstrate how occurrence typing works with local bindings of type Any.

You can also write it with clojure.core.typed/let like so:

(let [a :- Any, 1]
  (cond
    (symbol? a) a
    (number? a) a))