net*_*tom 1 haskell types generic-programming
我有一个很大的类型
data Value
= VNull
| VDouble !Double
| VSci !Scientific
| VInt !Int
| VText !Text
| VTexts ![Text]
| VByteString !BS.ByteString
| VUTCTime !UTCTime
-- This goes on for quite a few more lines
Run Code Online (Sandbox Code Playgroud)
我需要一个Hashable实例用于此数据类型.我当然可以手动输入实例,但幸运的是有一个基于泛型的hashWithSalt的默认实现.
不幸的是 - 据我所知 - 这需要任何可以在Value类型中"打包"的类型才能拥有Hashable实例.那么,UTCTime没有.
所以看起来我可以在两个"次优"解决方案之间做出选择:
我认为应该有第三种"最佳"方式:只为值构造函数编写一个无法自动执行的实现,即执行以下操作:
instance Hashable Value where
hashWithSalt (VUTCTime t) = ... -- custom implementation
hashWithSalt _ = ... -- use the default implementation
Run Code Online (Sandbox Code Playgroud)
当然可以更普遍地提出问题:如何在某些值构造函数的情况下重用现有的实例实现,同时在特定情况下拥有自己的实现,而无需为每个值构造函数编写样板文件.
对于这种特殊情况,您应该使用hashable-time包,它在标准化的位置定义孤立实例.
总的来说,对于这种情况,我要么:
newtype,这样您就可以在本地定义实例,而不会冒孤立实例的风险.newtype会给出的冗余.