Hacker News new | ask | show | jobs
by ryanbrush 4672 days ago
A perhaps naive question: could this sort of thing tap into Clojure's type hints rather than using external annotations? So rather than writing the example in the post:

(ann my-inc [Number -> Number])

(defn my-inc [n] (let [internal-number (Integer/parseInt "1")] (+ n internal-number)))

one could write this:

(defn ^Number my-inc [^Number n] (let [internal-number (Integer/parseInt "1")] (+ n internal-number)))

...and then run a type checker that reads and analyzes the hints?

Of course, the core.typed annotations could preserve whatever information they need in the compiled output, whereas the type hints aren't preserved past the compile phase (as far as I can tell.) Is that the limitation that requires defining type information in separate annotations?

2 comments

Clojure's type hints are only about Java interop and performance, and pretending otherwise is not a good idea. They aren't nearly rich enough to do the things that Typed Clojure does - think about less trivial examples like type checking heterogenous maps.
Ah, I didn't realize that core.typed could deal with rich data structures like that. That's actually pretty amazing.
I wanted something like this, so I made this library that checks annotations at run time rather than compile time.

https://github.com/steveshogren/deft

Right now it only checks maps for a set of keys, the idea longer term is to add in other types of "type" checking.

I haven't yet tied into the built-in annotations, just because those didn't appear to check anything but valid Java types. If it is possible, I would be really interested in seeing how.