Heinrich Apfelmus wrote: > Job Vranish wrote: >> I've been in a situation a lot lately where I need to keep a collection of >> values, and keep track of them by a persistent index. >> > > module Store (Key, Store, empty, add, delete, lookup) where > > newtype Key = Key { int :: Int } > > empty :: Store a > add :: a -> Store a -> (Key, Store a) > delete :: Key -> Store a -> Store a > lookup :: Key -> Store a -> Maybe a > > This way, the user doesn't know and care how Key is implemented. > > Last but not least, there is the issue that trying to use an already > deleted key might yield a wrong result instead of an error. That > shouldn't happen if used correctly, but might give a headache when > debugging.
There is even a very simple way to prevent at least some cases of misuse, when one key is accidentally used on stores of different type. A phantom parameter will do the trick: newtype Key a = Key { int :: Int } add :: a -> Store a -> (Key a , Store a) delete :: Key a -> Store a -> Store a lookup :: Key a -> Store a -> Maybe a Regards, apfelmus -- http://apfelmus.nfshost.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe