Fork me on GitHub

I have a strange question that might not make sense, but does cljs have a way to check whether two given immutable values share the same “parent” in its structural sharing “chain”?

(def m {:a 1})
(common-parent? m (assoc m :b 1))
;; => true
I realize this is a really weird ask, and probably not something I should be doing 😅


although you could use metadata to track this (def m ^{:foo 1} {:a 1}) (meta (assoc m :b 1)). as long as you use assoc/dissoc/update and stuff the metadata will remain

👆 1

Yea that’s interesting. Maybe store some kind of generated id in the metadata for comparison later:

(def m ^{:id (str (gensym))} ...)


perhaps could still be useful for your goals!


I’ll check it out, thanks!


It seems like metadata can probably help you. Just watch out for all the gotchas in the cases where metadata can disappear.


Yea definitely. Basically sticking to core functions and never accidentally recreating values from scratch? I’ve noticed in UI dev, even without metadata involved, I tend to think about the lifetimes of my values to ensure that prop diff checking remains very fast. I imagine metadata survival is a similar problem.

👌 1

@cjsauer no. in your example you'd get array maps where the array is just cloned and the resulting map maintains no relation to its "parent"


Ok thank you, makes sense.