-
-
Save jeroenvandijk/e8192dfda79aceeb557f to your computer and use it in GitHub Desktop.
core.typed use case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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