Fork me on GitHub
#core-typed
<
2022-09-12
>
Francesco Arena16:09:01

Hi everyone, I just started playing with typed clojure and I mostly studied from the wiki section of the old repo core.typed. I have a question about HMaps: is there a way to "merge" 2 (or more) hmaps to obtain a new one? In other words, is there a function mergeHmap that works like this:

(t/defalias A (t/HMap :mandatory {:a String :b Integer}))
(t/defalias B (t/HMap :mandatory {:c String}))
(t/defalias AB (mergeHmap A B)) ; => (t/HMap :mandatory {:a String :b Integer :c String})
My problem is: let's say we have two HMaps with several mandatory keys. It would be nice to have an easy way to obtain a "merged" version. Thanks for the help!

ambrosebs19:09:10

Hi @U03CEA0LVHP, this isn't exposed, but you might try intersecting them with t/I. It's different than a merge though, it results in a type that is "both" maps. Merging functionality is probably exposed in the internals, could be exposed as something like t/Merge perhaps.

ambrosebs19:09:43

looks like there's dissoc, assoc, and get implemented internally, but not merge. Would be cool to have. There's a helper to merge two HMap's, I wonder how much more is needed? https://github.com/typedclojure/typedclojure/blob/fdfbaa45f5e54118db72c4f3b4babb537421f518/typed/clj.checker/src/typed/clj/checker/assoc_utils.clj#L181

ambrosebs19:09:27

Oh, that must be the support for type checking clojure.core/merge calls.

ambrosebs19:09:36

Maybe we can just reify that as a type. Might not need any subtyping rules in the common case. If we can expand it to another HMap, it should just work.

ambrosebs03:09:24

released in 1.0.32 as t/Merge.