I have a record in a document that use two IntMaps:
data Doc = Doc { kernels :: IntMap Kernel, nodes :: IntMap Node }
But I found that the keys from both IntMaps have different meaning and I fail to separate in two differents types and don’t get errors when mix kernel types and node types. I want to have functions that check the the keys from kernel map and node maps and don’t allow mix-up. E.g:
someFunction :: Doc -> KernelKey -> NodeKey -> a
someFunction doc k1 k2 = .....
Instead of current:
someFunction :: Doc -> Int -> Int -> a
someFunction doc k1 k2 = .... -- warning check twice k1 and k2
Is it posible? Or I shall change from IntMap to Map.
Thanks
You can use
newtypeto make wrappers aroundIntto distinguish their meaning.That way, you can still use
IntMapinternally, but expose a more type-safe interface, especially if you also control how theKernelKeyandNodeKeyvalues get created, i.e. you don’t export their constructors so users only get them as return values from your other functions.Note that
newtypewrappers disappear at run time, so this extra wrapping and unwrapping does not affect performance in any way.