我正在尝试使用具有消化功能的自定义数据类型。
此数据类型与数据库ID字段相关。我将其定义为:
dbkey :: (Monad m, Monoid v) => Maybe DBKey -> Form v m DBKey
dbkey p = Pure . Singleton $ (fromMaybe (DBKey 0) p)
现在剩下的问题是在“表单”中检索此值。我不能使用fieldInputText
,因为它需要一个Text
字段-commentId: expected (Text _), but got: (Singleton _)
。
如何实现以上目标?必须编写自己的fieldInputText
版本似乎很复杂。
更新到目前为止,我有以下内容:
fieldInputSingleton :: forall a v. Text -> View v -> Proxy a -> a
fieldInputSingleton ref (View _ _ form input _ method) p =
queryField path form eval'
where
path = toPath ref
givenInput = lookupInput path input
eval' field = case field of
Singleton t -> evalField method givenInput (Singleton t)
f -> error $ T.unpack ref ++ ": expected (Singleton _), " ++ "but got: (" ++ show f ++ ")"
但是我遇到了:
• Couldn't match type ‘a’ with ‘b’
‘a’ is a rigid type variable bound by
the type signature for:
fieldInputSingleton :: forall a v. Text -> View v -> Proxy a -> a
at /home/chris/Projects/Haskell/Blog/src/View2.hs:26:1-65
‘b’ is a rigid type variable bound by
a type expected by the context:
forall b. Field v b -> a
at /home/chris/Projects/Haskell/Blog/src/View2.hs:28:5-30
Expected type: Field v b -> a
Actual type: Field v a -> a
• In the third argument of ‘queryField’, namely ‘eval'’
In the expression: queryField path form eval'
In an equation for ‘fieldInputSingleton’:
fieldInputSingleton ref (View _ _ form input _ method) p
= queryField path form eval'
where
path = toPath ref
givenInput = lookupInput path input
eval' field
= case field of
Singleton t -> evalField method givenInput (Singleton t)
f -> error
$ T.unpack ref
++ ": expected (Singleton _), " ++ "but got: (" ++ show f ++ ")"
• Relevant bindings include
eval' :: Field v a -> a
(bound at /home/chris/Projects/Haskell/Blog/src/View2.hs:32:5)
p :: Proxy a
(bound at /home/chris/Projects/Haskell/Blog/src/View2.hs:27:56)
fieldInputSingleton :: Text -> View v -> Proxy a -> a
(bound at /home/chris/Projects/Haskell/Blog/src/View2.hs:27:1)
|
28 | queryField path form eval'
| ^^^^^