Skip to content

Commit da5e88d

Browse files
committed
Merge pull request #411 from mpenet/feature/record-metadata
add metadata support to records
2 parents 1533fec + d613d13 commit da5e88d

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

pixie/stdlib.pxi

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ and implements IAssociative, ILookup and IObject."
12811281
fields (transduce (map (comp keyword name)) conj field-syms)
12821282
type-from-map `(defn ~map-ctor-name [m]
12831283
(apply ~ctor-name (map #(get m %) ~fields)))
1284+
meta-gs (gensym "meta")
12841285
default-bodies ['IAssociative
12851286
(-make-record-assoc-body ctor-name fields)
12861287

@@ -1299,6 +1300,13 @@ and implements IAssociative, ILookup and IObject."
12991300
[k `(get-field ~self-nm ~k-nm)])
13001301
fields)
13011302
not-found#)))
1303+
1304+
'IMeta
1305+
`(-with-meta [self# ~meta-gs]
1306+
(new ~nm
1307+
~@(conj field-syms meta-gs)))
1308+
`(-meta [self#] __meta)
1309+
13021310
'IObject
13031311
`(-str [self#]
13041312
(str "<" ~(name nm) " " (reduce #(assoc %1 %2 (%2 self#)) {} ~fields) ">"))
@@ -1310,9 +1318,13 @@ and implements IAssociative, ILookup and IObject."
13101318
`(-hash [self]
13111319
(hash [~@field-syms]))
13121320
`IRecord]
1313-
deftype-decl `(deftype ~nm ~fields ~@default-bodies ~@body)]
1321+
deftype-decl `(deftype ~nm ~(conj fields '__meta) ~@default-bodies ~@body)
1322+
ctor `(defn ~ctor-name ~field-syms
1323+
(new ~nm
1324+
~@(conj field-syms nil)))]
13141325
`(do ~type-from-map
1315-
~deftype-decl)))
1326+
~deftype-decl
1327+
~ctor)))
13161328

13171329
(defn print
13181330
{:doc "Prints the arguments, seperated by spaces."

tests/pixie/tests/test-defrecord.pxi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@
5353
(t/assert (contains? t :one))
5454
(t/assert (contains? t :two))
5555
(t/assert (contains? t :three))))
56+
57+
(t/deftest test-record-metadata
58+
(t/assert= nil (meta t1))
59+
(t/assert= :foo (-> t1 (with-meta :foo) meta)))

0 commit comments

Comments
 (0)