我想使用这样的派生实例:
data Test3D = forall a. (Show a, Eq a, Typeable a, Generic a)
=> Test3D { testDt :: String
, testPrm :: a
}
deriving (Show, Eq, Typeable, Generic)
instance Binary (Test3D)
$(deriveJSON defaultOptions ''Test3D)
但我从 GHC 收到:
• Can't make a derived instance of ‘Show Test3D’:
Constructor ‘Test3D’ has existentials or constraints in its type
Possible fix: use a standalone deriving declaration instead
• In the data declaration for ‘Test3D’
这种方式对于我的项目来说非常方便。我找不到解决方案。
有什么方法可以使用此类数据的派生实例吗?
最佳答案
Is any way of using derived instance for such data?
是的。按照 GHC 的建议,创建一个独立派生子句:
{-# LANGUAGE StandaloneDeriving, ExistentialQuantification #-}
data Test3D = forall a. (Show a)
=> Test3D { testDt :: String
, testPrm :: a
}
deriving instance Show Test3D
您不能做的是派生一个 Eq
实例,因为不同的值实际上可能包含不同的类型,并且只能通过 Typeable
将它们与动态转换 hack 进行比较.
关于haskell - Haskell 中的派生实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46031533/