core-typed

Francesco Arena 2022-09-12T16:25:01.075629Z

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!

2022-09-17T03:47:24.225819Z

released in 1.0.32 as t/Merge.

2022-09-12T19:18:10.826649Z

Hi @ciccio, 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.

2022-09-12T19:27:43.456579Z

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

2022-09-12T19:28:27.145649Z

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

2022-09-12T19:29:36.847109Z

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.