Haskell map where key type defines value type

Is there a way to use Data.Map so that the type of value depends on the type of key? I need to make sure that I never insert the wrong printed value on the map.

for example: Let's say I have types KeyType aand ValType a then the Map should have a type similar to

Map (KeyType a) (ValType a)

But this clearly does not work, because it is anot connected.

+4
source share
1 answer

You can use a type synonym for binding aso that it is the same for yours KeyTypeand ValType:

type MapFor a = Map (KeyType a) (ValType a)

Now you can use MapFor a(for some specific a) anywhere you would use Map (KeyType a) (ValType a).

0
source

Source: https://habr.com/ru/post/1676711/


All Articles