我有一个模块Foo.hs
其中包含不派生Generic
一个定义:同时派生Generic和ToJSON?
-- Foo.hs
data Blather = Blather ... -- Generic not derived here
而在另一个模块我想获得ToJSON
:
-- Bar.hs
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
import GHC.Generics
import Data.Aeson
instance Generic Blather
instance ToJSON Blather
但它不会编译。如果我在定义站点的Foo.hs
中推导出Generic,则我可以稍后在另一个模块中推导出ToJSON
。
我可以在Bar.hs
中得出ToJSON Blather
而无需修改原始Foo.hs
?
或者有没有简单的方法手写instance ToJSON Blather
?
它是否与['StandaloneDeriving'分机]工作(https://downloads.haskell.org/~ghc/7.8.4/docs/html/users_guide/deriving.html )? –
为什么你不想在你定义类型的地方派生'Generic'?孤儿实例是邪恶的! – dfeuer
是的,这将是正确的解决方案,但在此时我不想提交修改'Foo.hs'的PR。 – ErikR