haskell - 子站点类型之谜
问题描述
Foundation.hs
从身份验证插件实现类型类时,我无法弄清楚我应该使用哪些类型/它使用了身份验证子站点:
我能感觉到我很接近,但我缺乏理解。我只是想为登录/注册页面使用不同的布局。
在 Foundation.hs 中:
instance YesodAuthSimple App where
type AuthSimpleId App = UserId
...
-- Works
onRegisterSuccess :: YesodAuthSimple site => AuthHandler site Html
onRegisterSuccess = authLayout $ [whamlet|
$newline never
<div>
<h1>You Registered successfully.
<p>
Some text here.
|]
-- Works when I do not write a type signature
loginTemplate toParent mErr = $(widgetFile "authpartials/login")
-- Does not work with or without type signatures
customAuthLayout widget = do
master <- getYesod
mmsg <- getMessage
muser <- maybeAuthPair
mcurrentRoute <- getCurrentRoute
pc <- widgetToPageContent $ do
$(widgetFile "custom-auth-layout")
withUrlRenderer $(hamletFile "templates/default-layout-wrapper.hamlet")
432:15 指的是widgetToPageContent
通话。
在类型类定义 Simple.hs 中:
class (YesodAuth site, PathPiece (AuthSimpleId site)) => YesodAuthSimple site where
type AuthSimpleId site
...
customAuthLayout :: WidgetFor site () -> AuthHandler site Html
...
我粘贴了customAuthLayout
来自defaultLayout
Foundation.hs的定义
这是我从 GHC 得到的错误:
Foundation.hs:432:15: error:
• Could not deduce: m ~ HandlerFor App
from the context: MonadAuthHandler App m
bound by the type signature for:
customAuthLayout :: WidgetFor App () -> AuthHandler App Html
at src/Foundation.hs:(427,5)-(434,79)
‘m’ is a rigid type variable bound by
the type signature for:
customAuthLayout :: WidgetFor App () -> AuthHandler App Html
at src/Foundation.hs:(427,5)-(434,79)
Expected type: m (PageContent (Route App))
Actual type: HandlerFor App (PageContent (Route App))
• In a stmt of a 'do' block:
pc <- widgetToPageContent
$ do (do do (asWidgetT GHC.Base.. toWidget)
((blaze-markup-0.8.2.2:Text.Blaze.Internal.preEscapedText
GHC.Base.. Data.Text.pack)
"<!-- custom-auth-layout -->
<body class="d-flex align-items-center bg-auth border-top border-top-2 border-primary">")
....)
In the expression:
do master <- getYesod
mmsg <- getMessage
muser <- maybeAuthPair
mcurrentRoute <- getCurrentRoute
....
In an equation for ‘customAuthLayout’:
customAuthLayout widget
= do master <- getYesod
mmsg <- getMessage
muser <- maybeAuthPair
....
|
432 | pc <- widgetToPageContent $ do
| ^^^^^^^^^^^^^^^^^^^^^^^^...
我已经成功地将本教程用于普通(非子站点页面)https://ersocon.net/cookbooks/yesod/html-and-seo/custom-layouts
但是我被子站点类型绊倒了。我已阅读 Michael Snoyman 关于子站点类型的非常好的旧博客文章,但我无法理解 GHC 的错误消息。
我怀疑 Simple.hs 中的类型签名是错误的,或者我在函数定义中遗漏了一些东西。
解决方案
尝试在liftHandler
之前添加widgetToPageContent
:
...
pc <- liftHandler $ widgetToPageContent $ do
$(widgetFile "custom-auth-layout")
...
错误消息中的关键行是:
Could not deduce: m ~ HandlerFor App
...
Expected type: m (PageContent (Route App))
Actual type: HandlerFor App (PageContent (Route App))
它基本上告诉我们它期望一个更通用的类型m
,但它却得到了一个HandlerFor App
. 所以解决方案只是取消调用widgetToPageContent
使用该liftHandler
函数。
更详细地说,如果我们查看函数的类型签名widgetToPageContent
,我们会看到它返回HandlerFor site (PageContent (Route site))
。在这种情况下,site
实例化为App
,这就是HandlerFor App (PageContent (Route App))
您在错误消息中看到的。
同样,您的customLayout
函数返回AuthHandler site Html
. AuthHandler
只是一个类型同义词,它限制site
到一个等价的类型,HandlerSite m
它也是YesodAuth
. 这也解析为App
,这就是我们在错误消息中得到MonadAuthHandler App m
和的原因。m (PageContent (Route App))
推荐阅读
- php - laravel 根据表数据进行验证
- c++ - 当我在交换两个字符串时访问正确的内存时遇到了分段错误
- uml - 如果我在模型中有确切数量的东西,我应该使用 n 还是 *?
- azure - 带有 Sitecore 和 Dynamics 365 门户的 Azure AD B2C SSO
- mysql - MySQL 选择查询非常慢
- python - 使用 for 循环时出现 ValueError [已解决]
- vue.js - 如何使 bootstrap4 崩溃在 vue 中工作?
- r - R中ROSE的过采样和欠采样问题
- python - 这段python字典代码有什么问题?
- macos - 导出 DYLD_LIBRARY_PATH 导致 libncurses.5.4.dylib 未加载错误