Skip to content

Commit 7814752

Browse files
committed
DynamicObjectInstance: Wrap the return value of cons
1 parent aaf530c commit 7814752

File tree

2 files changed

+2
-35
lines changed

2 files changed

+2
-35
lines changed

README.md

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -458,40 +458,6 @@ IllegalStateException The following fields had the wrong type:
458458
=> #LL{:next #LL{:next #LL{:value 3, :next nil}, :value 2}, :value 1}
459459
```
460460

461-
It is important to note that some Clojure functions only return plain Clojure maps.
462-
463-
```clojure
464-
; What about Clojure functions that are hard-coded to return plain Clojure maps?
465-
(conj tail [:key :value])
466-
=> {:key :value, :value 3, :next nil}
467-
(class *1)
468-
=> clojure.lang.PersistentArrayMap
469-
470-
; In this case, the returned map can simply be wrapped again:
471-
(DynamicObject/wrap *1 RecursionTest$LinkedList)
472-
=> #LL{:key :value, :value 3, :next nil}
473-
(.getType *1)
474-
=> com.github.rschmitt.dynamicobject.RecursionTest$LinkedList
475-
476-
; Some functions recurse over the data and return nested maps:
477-
(def new-head (assoc-in head [:next :next :value] 19))
478-
=> #'user/new-head
479-
(def list-diff (clojure.data/diff head new-head))
480-
=> #'user/list-diff
481-
(pprint list-diff)
482-
({:next {:next {:value 3}}}
483-
{:next {:next {:value 19}}}
484-
{:next {:next {:next nil}, :value 2}, :value 1})
485-
486-
; However, DynamicObject's getter methods will restore the stripped type information just in time:
487-
(DynamicObject/wrap (nth list-diff 2) RecursionTest$LinkedList)
488-
=> #LL{:next {:next {:next nil}, :value 2}, :value 1}
489-
(.next *1)
490-
=> #LL{:next {:next nil}, :value 2}
491-
(.next *1)
492-
=> #LL{:next nil}
493-
```
494-
495461
## Guidelines
496462

497463
* Always register a reader tag for any `DynamicObject` that will be serialized. This reader tag should be namespaced with some appropriate prefix (e.g. a Java package name), as all unprefixed reader tags are reserved for future use by the Edn specification.

src/main/java/com/github/rschmitt/dynamicobject/internal/DynamicObjectInstance.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ public int count() {
234234

235235
@Override
236236
public IPersistentCollection cons(Object o) {
237-
return ((IPersistentCollection) map).cons(o);
237+
Map newMap = (Map) ((IPersistentCollection) map).cons(o);
238+
return (DynamicObjectInstance) DynamicObject.wrap(newMap, type);
238239
}
239240

240241
@Override

0 commit comments

Comments
 (0)