diff --git a/src/sci/impl/records.cljc b/src/sci/impl/records.cljc index 06fa54ba..d582494b 100644 --- a/src/sci/impl/records.cljc +++ b/src/sci/impl/records.cljc @@ -6,7 +6,9 @@ [sci.impl.vars :as vars])) (defn defrecord [_ _ ctx record-name fields & protocol-impls] - (let [factory-fn-sym (symbol (str "->" record-name)) + (let [factory-fn-str (str "->" record-name) + factory-fn-sym (symbol factory-fn-str) + map-factory-sym (symbol (str "map" factory-fn-str)) keys (mapv keyword fields) protocol-impls (utils/split-when symbol? protocol-impls) protocol-impls @@ -32,6 +34,16 @@ impls))) protocol-impls)] `(do + (defn ~factory-fn-sym [& args#] + (vary-meta (zipmap ~keys args#) + assoc + :sci.impl/record true + :sci.impl/type '~record-name)) + (defn ~map-factory-sym [m#] + (vary-meta m# + assoc + :sci.impl/record true + :sci.impl/type '~record-name)) (defn ~factory-fn-sym [& args#] (vary-meta (zipmap ~keys args#) assoc diff --git a/test/sci/records_test.cljc b/test/sci/records_test.cljc index e15ff153..a704c2b1 100644 --- a/test/sci/records_test.cljc +++ b/test/sci/records_test.cljc @@ -5,9 +5,10 @@ (deftest protocol-test (let [prog " (defrecord Foo [a b]) -(let [r (->Foo 1 2)] - [(:a r) (:b r)])"] - (is (= [1 2] (tu/eval* prog {})))) +(let [r1 (->Foo 1 2) + r2 (map->Foo {:a 1 :b 2})] + [(:a r1) (:b r1) (:a r2) (:b r2)])"] + (is (= [1 2 1 2] (tu/eval* prog {})))) (testing "protocols" (let [prog " (ns foo)