用字母替换字段名称,我有这样的情况:
data Foo = Foo { a :: Maybe ...
, b :: [...]
, c :: Maybe ...
, ... for a lot more fields ...
} deriving (Show, Eq, Ord)
instance Writer Foo where
write x = maybeWrite a ++
listWrite b ++
maybeWrite c ++
... for a lot more fields ...
parser = permute (Foo
<$?> (Nothing, Just `liftM` aParser)
<|?> ([], bParser)
<|?> (Nothing, Just `liftM` cParser)
... for a lot more fields ...
-- …Run Code Online (Sandbox Code Playgroud) 有没有办法在类型系统中轻松完成以下操作?
data Product = Product {
id :: ProductId
, name :: Text
, sku :: SKU, quantity :: Int
, description :: Maybe Text
}
data Omittable a = Omit | Present a
type ProductWithOmittableFields = Omittable Product
-- ProductWithOmittableFields is now equivalent to:\
--
-- data ProductWithOmittableFields = ProductWithOmmitableFields {
-- id :: Omittable ProductId
-- ,name :: Omittable Text
-- ,sku : : Omittable SKU
-- ,quantity :: Omittable Int
-- ,desciption :: Omittable (Maybe Text)
-- …Run Code Online (Sandbox Code Playgroud)