Create a gist now

Instantly share code, notes, and snippets.

@pnf /typed-error.clj Secret
Last active Dec 22, 2015

(ns imdb.testassert
(:require [clojure.core.typed :as t]))
;; This should and does work.
(t/ann testassert1 [(HMap) -> (HMap :mandatory {:foo (t/Seqable t/AnyInteger)})])
(defn testassert1 [m]
(assert (vector? (:foo m)))
(assert (every? integer? (:foo m)))
m
)
;; I think this should give me an error, as there's been no assertion about the contents of
;; the :foo value, but it does not.
(t/ann testassert2 [(HMap) -> (HMap :mandatory {:foo (t/Seqable t/AnyInteger)})])
(defn testassert2 [m]
(assert (vector? (:foo m)))
m
)
;; Using (Map Any Any) instead of (HMap) results in errors that I don't understand.
(t/ann testassert3 [(t/Map Any Any) -> (HMap :mandatory {:foo (t/Seqable t/AnyInteger)})])
(defn testassert3 [m] ;23
(assert (vector? (:foo m))) ;24
(assert (every? integer? (:foo m))) ;25
m
)
;; Type Error (imdb.testassert:23:11) Polymorphic function clojure.core/every? could not be applied to arguments:
;; Polymorphic Variables:
;; x
;; y
;; Domains:
;; (Fn [x -> Any :filters {:then (is y 0), :else tt}]) (t/Coll x)
;; (Fn [x -> Any :filters {:then (is y 0), :else tt}]) (U nil (t/Coll x))
;; (Fn [x -> Any]) (U (clojure.lang.Seqable x) nil)
;; Arguments:
;; (Fn [Any -> boolean :filters {:then (is t/AnyInteger 0), :else (! t/AnyInteger 0)}]) Any
;; Ranges:
;; Boolean :filters {:then (is (t/Coll y) 1), :else tt}
;; Boolean :filters {:then (is (U (t/Coll y) nil) 1), :else tt}
;; Boolean
;; in: (clojure.core/every? clojure.core/integer? (:foo m))
;; in: (clojure.core/every? clojure.core/integer? (:foo m))
;; Type Error (imdb.testassert:21) Local binding m expected type (HMap :mandatory {:foo (t/Seqable t/AnyInteger)}), but actual type (IPersistentMap Any Any)
;; in: m
;; Type Error (imdb.testassert:21) Type mismatch:
;; Expected: (HMap :mandatory {:foo (t/Seqable t/AnyInteger)})
;; Actual: (IPersistentMap Any Any)
;; in: (do (if (clojure.core/vector? (:foo m)) nil (throw (new java.lang.AssertionError #))) (if (clojure.core/every? clojure.core/integer? (:foo m)) nil (throw (new java.lang.AssertionError #))) m)
;; Type Error (imdb.testassert:21:1) Type mismatch:
;; Expected: (Fn [(t/Map Any Any) -> (HMap :mandatory {:foo (t/Seqable t/AnyInteger)})])
;; Actual: (Fn [(t/Map Any Any) -> (IPersistentMap Any Any) :filters {:then (& (is (IPersistentMap Any Any) 0) (! (U nil false) 0)), :else (is (U nil false) 0)} :object {:id 0}])
;;in: (def testassert3 (fn* ([m] (do # # m))))
;; Get EXPECTED error, as nothing has been asserted.
(t/ann testassert4 [(t/Vec Any) -> (t/Vec t/AnyInteger)])
(defn testassert4 [m] ;72
m
)
;; Type Error (imdb.testassert:74) Local binding m expected type (t/Vec t/AnyInteger), but actual type (t/Vec Any)
;; in: m
;; Type Error (imdb.testassert:74) Type mismatch:
;; Expected: (t/Vec t/AnyInteger)
;; Actual: (t/Vec Any)
;; in: m
;; Type Error (imdb.testassert:74:1) Type mismatch:
;; Expected: (Fn [(t/Vec Any) -> (t/Vec t/AnyInteger)])
;; Actual: (Fn [(t/Vec Any) -> (t/Vec Any) :filters {:then (& (is (IPersistentVector Any) 0) (! (U nil false) 0)), :else (is (U nil false) 0)} :object {:id 0}])
;; in: (def testassert4 (fn* ([m] m)))
;; Again, the error is EXPECTED, since we never said what was in the vector.
(t/ann testassert5 [(t/Vec Any) -> (t/Vec t/AnyInteger)])
(defn testassert5 [m]
(assert (vector? m))
m
)
;; Type Error (imdb.testassert:74) Local binding m expected type (t/Vec t/AnyInteger), but actual type (t/Vec Any)
;; in: m
;; Type Error (imdb.testassert:74) Type mismatch:
;; Expected: (t/Vec t/AnyInteger)
;; Actual: (t/Vec Any)
;; in: (do (if (clojure.core/vector? m) nil (throw (new java.lang.AssertionError #))) m)
;; Type Error (imdb.testassert:74:1) Type mismatch:
;; Expected: (Fn [(t/Vec Any) -> (t/Vec t/AnyInteger)])
;; Actual: (Fn [(t/Vec Any) -> (t/Vec Any) :filters {:then (& (is (IPersistentVector Any) 0) (! (U nil false) 0)), :else (is (U nil false) 0)} :object {:id 0}])
;; in: (def testassert5 (fn* ([m] (do # m))))
;; There seems to be some difficulty in merging the two assertions here.
(t/ann testassert6 [(t/Vec Any) -> (t/Vec t/AnyInteger)])
(defn testassert6 [m]
(assert (vector? m)) ; this should be redundant
(assert (every? integer? m))
m)
;; Type Error (imdb.testassert:92) Local binding m expected type (t/Vec t/AnyInteger), but actual type (I (IPersistentVector Any) (t/Coll t/AnyInteger))
;; in: m
;; Type Error (imdb.testassert:92) Type mismatch:
;; Expected: (t/Vec t/AnyInteger)
;; Actual: (I (IPersistentVector Any) (t/Coll t/AnyInteger))
;; in: (do (if (clojure.core/vector? m) nil (throw (new java.lang.AssertionError #))) (if (clojure.core/every? clojure.core/integer? m) nil (throw (new java.lang.AssertionError #))) m)
;; Type Error (imdb.testassert:92:1) Type mismatch:
;; Expected: (Fn [(t/Vec Any) -> (t/Vec t/AnyInteger)])
;; Actual: (Fn [(t/Vec Any) -> (I (IPersistentVector Any) (t/Coll t/AnyInteger)) :filters {:then (& (is (IPersistentVector Any) 0) (! (U nil false) 0)), :else (is (U nil false) 0)} :object {:id 0}])
;; in: (def testassert6 (fn* ([m] (do # # m))))
;; There seems to be some difficulty in merging the two assertions here.
(t/ann testassert7 [Any -> (t/Vec t/AnyInteger)])
(defn testassert7 [m] ; line 92
(assert (vector? m))
(assert (every? integer? m))
m)
;; Type Error (imdb.testassert:115) Local binding m expected type (t/Vec t/AnyInteger), but actual type (I (t/Vec Any) (t/Coll t/AnyInteger))
;; in: m
;; Type Error (imdb.testassert:115) Type mismatch:
;; Expected: (t/Vec t/AnyInteger)
;; Actual: (I (t/Vec Any) (t/Coll t/AnyInteger))
;; in: (do (if (clojure.core/vector? m) nil (throw (new java.lang.AssertionError #))) (if (clojure.core/every? clojure.core/integer? m) nil (throw (new java.lang.AssertionError #))) m)
;; Type Error (imdb.testassert:115:1) Type mismatch:
;; Expected: (Fn [Any -> (t/Vec t/AnyInteger)])
;; Actual: (Fn [Any -> (I (t/Vec Any) (t/Coll t/AnyInteger)) :filters {:then (! (U nil false) 0), :else (is (U nil false) 0)} :object {:id 0}])
;; in: (def testassert7 (fn* ([m] (do # # m))))
;; No error.
(t/ann testassert8 [Any -> (t/Vec Any)])
(defn testassert8 [m]
(assert (vector? m))
m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment