我有一个Yesod应用程序。我当前的目标是让应用程序使用电子邮件/密码进行身份验证。但是,我遇到类型错误。
我相信我正确地遵循了Yesod Book中的指示。这是我当前的code for Foundation.hs。
错误如下:
• Could not deduce: m ~ HandlerFor site0
from the context: (MonadHandler m, HandlerSite m ~ master)
bound by the type signature for:
getAuthId :: forall (m :: * -> *) master.
(MonadHandler m, HandlerSite m ~ master) =>
Creds master -> m (Maybe (AuthId master))
at src/Foundation.hs:206:18-102
‘m’ is a rigid type variable bound by
the type signature for:
getAuthId :: forall (m :: * -> *) master.
(MonadHandler m, HandlerSite m ~ master) =>
Creds master -> m (Maybe (AuthId master))
at src/Foundation.hs:206:18-102
Expected type: m (Maybe (AuthId master))
Actual type: HandlerFor site0 (Maybe (Key User))
• In the expression:
runDB
$ do x <- insertBy $ User (credsIdent creds) Nothing Nothing False
return
$ Just
$ case x of
Left (Entity userid _) -> userid
Right userid -> userid
In an equation for ‘getAuthId’:
getAuthId creds
= runDB
$ do x <- insertBy $ User (credsIdent creds) Nothing Nothing False
return
$ Just
$ case x of
Left (Entity userid _) -> userid
Right userid -> userid
In the instance declaration for ‘YesodAuth App’
• Relevant bindings include
getAuthId :: Creds master -> m (Maybe (AuthId master))
(bound at src/Foundation.hs:207:5)
|
| getAuthId creds = runDB $ do
| ^^^^^^^^^^...
我关注的部分是:
Expected type: m (Maybe (AuthId master))
Actual type: HandlerFor site0 (Maybe (Key User))
这就是我(认为)我所知道的:
注意:我假设模板Haskell基于Persistent chapter of the Yesod Book(在该页面上搜索type UserId = Key User
)生成了类型别名PersonId
,并且我的模型文件包含以下内容: :
-- By default this file is used by `persistFileWith` in Model.hs (which is imported by Foundation.hs)
-- Syntax for this file here: https://github.com/yesodweb/persistent/blob/master/docs/Persistent-entity-syntax.md
User
email Text
password Text Maybe
verkey Text Maybe
verified Bool
UniqueUser email
deriving Typeable
如果我进行所有这些替换,则得出的结论是预期类型为m (Maybe (AuthId master))
,它等效于m (Maybe UserId)
,等效于m (Maybe (Key User))
。对于实际类型,HandlerFor site0 (Maybe (Key User))
等效于m (Maybe (Key User))
。在我看来,期望的类型和实际的类型是相同的,但是编译器并不能得出相同的结论。
我想知道在这里我需要做些什么来解决编译器遇到的问题。
P.S。我最初将此问题发布在Reddit上。通常,我不会发布到两个不同的地方,但这使我无法继续前进。