Skip to content

Commit

Permalink
Fix/Unbreak accidental generative record accessors.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Frese committed Jun 5, 2024
1 parent d4f6ae5 commit db6e5ca
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/active/data/raw_record.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
(defn- is-exactly?-0 [t-1 t-2]
(= t-1 t-2))

(defn- is-exactly?-2 [t-1 t-2]
#_(defn- is-exactly?-2 [t-1 t-2]
(identical? t-1 t-2))

(defn- is-exactly-a?-0 [t m]
Expand All @@ -137,7 +137,7 @@
(or (is-exactly?-0 parent-t et)
(is-extension-of?-0 parent-t et))))

(defn- is-extension-of?-2 [parent-t child-t]
#_(defn- is-extension-of?-2 [parent-t child-t]
;; is the record child-t a direct or indirect extension of parent-t?
(when-let [et (:extends (struct-type/variant child-t))]
(or (is-exactly?-2 parent-t et)
Expand All @@ -163,12 +163,12 @@

(defn- struct-type-matcher [record]
;; Note: this enables optimized access to keys in extended records; extended
;; keys must be come first for this, so the indices are the same as in the parent record!
;; keys must come first for this, so the indices are the same as in the parent record!
(fn [struct-type]
;; This may return false negatives (e.g. after hot code reload) as an optimization for the normal case.
(or (is-exactly?-2 record struct-type)
;; If this returns false, an exception is thrown in the optimized accessors.
(or (is-exactly?-0 record struct-type)
(and (record? struct-type)
(is-extension-of?-2 record struct-type)))))
(is-extension-of?-0 record struct-type)))))

(defn ^:no-doc accessor [record key]
(struct-map/accessor* record key (struct-type-matcher record)))
Expand Down
4 changes: 2 additions & 2 deletions test/active/data/benchmarks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
(r/def-record ADRecord [r-a r-b r-c r-d r-e])
(def mk-ad (r/constructor ADRecord))

(def get-a r-a #_ (active.data.struct/accessor ADRecord r-a))
(def get-a r-a)

(defrecord JRecord [a b c d e])

(r/def-record Spec [r-a])
(r/def-record Spec [s-a])
(def spec (r/constructor Spec))

(defn -main []
Expand Down
16 changes: 11 additions & 5 deletions test/active/data/raw_record_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@

#?(:clj
(t/deftest non-generative-clj-test
(let [[t1 v1] (eval '(vector (active.data.raw-record/def-record A [a])
(A a :foo)))
[t2 v2] (eval '(vector (active.data.raw-record/def-record A [a])
(A a :foo)))]

(let [[t1 v1 get-1] (eval '(vector (active.data.raw-record/def-record A [a])
(A a :foo)
a))
[t2 v2 get-2] (eval '(vector (active.data.raw-record/def-record A [a])
(A a :foo)
a))]

;; records and instances are equal
(t/is (= t1 t2))

Expand All @@ -65,6 +67,10 @@
;; both values are exact instances of the "other" record
(t/is (sut/is-exactly-a? t1 v2))
(t/is (sut/is-exactly-a? t2 v1))

;; access works too either way
(t/is (= :foo (get-1 v2)))
(t/is (= :foo (get-2 v1)))
)))

(t/deftest reflection-test
Expand Down

0 comments on commit db6e5ca

Please sign in to comment.