我有一个这样的数据类型:
module My.Module
data A = A { aFoo :: Integer } deriving (Generic, Show)我有伊森的通用选择权
import Data.Char ( toUpper, toLower )
genericOptions :: String -> Options
genericOptions prefix = defaultOptions
{ fieldLabelModifier = dropPrefix $ length prefix
, constructorTagModifier = addPrefix prefix
, omitNothingFields = True
}
where
dropPrefix l s = let remainder = drop l s
in (toLower . head) remainder : tail remainder
addPrefix p s = p ++ toUpper (head s) : tail s所以我可以像这样使用它
instance A.FromJSON A where
parseJSON = A.genericParseJSON $ genericOptions "A"
instance A.ToJSON A where
toJSON = A.genericToJSON $ genericOptions "A"但我意识到我可以用一些模板haskell
import Data.Aeson.TH ( deriveJSON )
import Language.Haskell.TH.Syntax ( Dec, Name, Q )
genericDeriveJSON :: Name -> Q [Dec]
genericDeriveJSON name =
deriveJSON (genericOptions (show name)) name
$(genericDeriveJSON ''A)它抛出一个错误:
Exception when trying to run
compile-time code:
Prelude.tail: empty list
Code: A.genericDeriveJSON ''AdropPrefix上的drop l s似乎返回了一个空字符串,这意味着show name的值不是字符串"A“。既然我不认为我可以检查这个值,有人知道它的值是多少吗?
发布于 2019-08-25 00:44:20
尝试使用nameBase而不是show (它用于调试,而不是核心逻辑)。
为了了解show正在做什么,您可以查看定义为showName的implementation of show,它本身定义为showName' Alone,粗略地了解它构造了您的类型的完全限定名。
https://stackoverflow.com/questions/57635686
复制相似问题