Combining HMap with another map type

15 views
Skip to first unread message

Kaylen Wheeler

unread,
Mar 7, 2016, 1:18:56 AM3/7/16
to core.typed
I'm looking for a way to define an HMap so that certain of its required keys have certain types, but any additional key/value pairs have different types.

For instance, let's say I want a map of String->Int with one additional key :id->UUID.  So, it the key :id is present, it must be a UUID.  All other keys must be string, and their values must be Int.

(So far additional HMap keys must be keywords.)

Is there a way to do this?

Ambrose Bonnaire-Sergeant

unread,
Mar 7, 2016, 3:26:09 PM3/7/16
to core.typed
In theory yes, but there might be some kinks to iron out in practice.

Try making an intersection type.

(I '{:id->UUID} (Map String Int))

Kaylen Wheeler

unread,
Mar 7, 2016, 5:15:28 PM3/7/16
to core.typed
That brings up another question.  I've seen the quoted map literals used in type definitions.  Is there a difference between that and HMap?

Ambrose Bonnaire-Sergeant

unread,
Mar 7, 2016, 5:16:32 PM3/7/16
to core.typed
'{...} is the same as (HMap :mandatory {...})

Kaylen Wheeler

unread,
Mar 8, 2016, 1:04:02 AM3/8/16
to core.typed
The attempt at making an intersection type didn't work.  I suppose the simple workaround is to move the rest of the map into a sub-map.

=> (t/cf (defalias UTest (I (t/HMap :mandatory {:x t/Int}) (t/Map String String))))

=> (t/cf {:x 1 "A" "B"} UTest)

Type mismatch:

Expected: UTest

Actual: (I (APersistentMap (U (t/Val :x) (t/Val "A")) (U (t/Val 1) (t/Val "B"))) (t/ExactCount 2))
in: {:x 1, "A" "B"}
Reply all
Reply to author
Forward
0 new messages