Skip to content

Commit

Permalink
[#431] Support map constructor for maps
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Oct 17, 2020
1 parent ecb4cba commit ac0b4a1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/sci/impl/records.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions test/sci/records_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ac0b4a1

Please sign in to comment.