From db6e5cafb2c9e7906e76236722fde7387bfa3bc9 Mon Sep 17 00:00:00 2001 From: David Frese Date: Wed, 5 Jun 2024 15:11:10 +0200 Subject: [PATCH] Fix/Unbreak accidental generative record accessors. --- src/active/data/raw_record.cljc | 12 ++++++------ test/active/data/benchmarks.clj | 4 ++-- test/active/data/raw_record_test.cljc | 16 +++++++++++----- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/active/data/raw_record.cljc b/src/active/data/raw_record.cljc index 09de085..2bfd902 100644 --- a/src/active/data/raw_record.cljc +++ b/src/active/data/raw_record.cljc @@ -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] @@ -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) @@ -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))) diff --git a/test/active/data/benchmarks.clj b/test/active/data/benchmarks.clj index 969d8f2..bf91fbb 100644 --- a/test/active/data/benchmarks.clj +++ b/test/active/data/benchmarks.clj @@ -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 [] diff --git a/test/active/data/raw_record_test.cljc b/test/active/data/raw_record_test.cljc index 9cca168..de44cdd 100644 --- a/test/active/data/raw_record_test.cljc +++ b/test/active/data/raw_record_test.cljc @@ -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)) @@ -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