Skip to content
Browse files

Generate inlining predicate for defconstrainedrecord + tests

Signed-off-by: fogus <mefogus@gmail.com>
  • Loading branch information...
1 parent b6239a4 commit 6af4be591211946909102051413b09d19e5a0126 @frenchy64 frenchy64 committed with fogus Jun 6, 2013
View
7 src/main/clojure/clojure/core/contracts/constraints.clj
@@ -144,15 +144,16 @@
ctor-name (symbol (str name \.))
positional-factory-name (symbol (str "->" name))
map-arrow-factory-name (symbol (str "map->" name))
+ pred-arg (gensym)
chk `(clojure.core.contracts/contract
~(symbol (str "chk-" name))
~inv-description
[{:keys ~fields :as m#}]
~invariants)]
`(do
- (let [t# (defrecord ~name ~fields ~@etc)]
- (defn ~(symbol (str name \?)) [r#]
- (= t# (type r#))))
+ (defrecord ~name ~fields ~@etc)
+ (defn ~(symbol (str name \?)) [~pred-arg]
+ (instance? ~name ~pred-arg))
~(build-positional-factory name classname fields invariants chk)
View
18 src/test/clojure/clojure/core/constraints_tests.clj
@@ -0,0 +1,18 @@
+(ns clojure.core.constraints-tests
+ (:use [clojure.test :only (deftest is)]
+ [clojure.core.contracts.constraints :only (defconstrainedrecord)]))
+
+(defconstrainedrecord Foo [a]
+ "My doc"
+ [])
+
+(deftest instance-predicate-test
+ (is (Foo? (->Foo 'a)))
+ (is (not (Foo? nil))))
+
+;(let [a (->Foo 'a)]
+; (time
+; (dotimes [_ 1e6]
+; (Foo? a))))
+; 17ms (instance?)
+; 185ms (type)

0 comments on commit 6af4be5

Please sign in to comment.
Something went wrong with that request. Please try again.