Skip to content

Instantly share code, notes, and snippets.

@jeroenvandijk
Last active November 26, 2015 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenvandijk/e8192dfda79aceeb557f to your computer and use it in GitHub Desktop.
Save jeroenvandijk/e8192dfda79aceeb557f to your computer and use it in GitHub Desktop.
core.typed use case
(require '[clojure.core.typed :as t])
;; Instead of via namespaces
(t/check-ns 'valid-ns)
(t/check-ns 'invalid-ns)
;; I would like to have something like this to work
(t/cf
(let [a (fn [x] x)
_ (t/ann ^:no-check a [Number -> Number])
b (fn [x] (a 1))
_ (t/ann-form b [Number -> Number])]
(b 1)))
(ns invalid-ns
(:require [clojure.core.typed :as t]))
;; Missing checks here
(defn unchecked-f [x]
(str "no-int"))
(let [a unchecked-f
b (fn [x] (a 1))
_ (t/ann-form b [Number -> Number])]
(b 1))
(t/cf
(let [a (t/cast [Number :-> Number] (fn [x] x))
b (fn [x] (a 1))
_ (t/ann-form b [Number -> Number])]
(b 1)))
;=> Number
(let [x (try
(t/cf
(let [a (t/cast [Number :-> Number] (fn [x] x))
b (fn [x] (a 1))
_ (t/ann-form b [Number -> Number])]
(b 1)))
(catch Exception e e))
]
(if-let [error (ex-data x)]
error
:ok))
;=> :ok
(let [x (try
(t/cf
(let [a (t/cast [String :-> Number] (fn [x] x))
b (fn [x] (a 1))
_ (t/ann-form b [Number -> Number])]
(b 1)))
;; Fixme Exception is still being printed here
(catch Exception e e))
]
(if-let [msg (some-> x ex-data :errors first .getMessage)]
msg
:ok))
;=> "Function a could not be applied to arguments:\n\n\nDomains:\n\tString\n\nArguments:\n\t(t/Val 1)\n\nRanges:\n\tNumber\n\n"
(ns valid-ns
(:require [clojure.core.typed :as t]))
(t/ann ^:no-check unchecked-f [Number -> Number])
(defn unchecked-f [x]
(str "no-int"))
(let [a unchecked-f
b (fn [x] (a 1))
_ (t/ann-form b [Number -> Number])]
(b 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment