語境
在控制器中,我有一個函式通過呼叫beforeAction來檢查某個欄位是否user為空。如果它為空,則用戶應該被重定向到另一個控制器中的某個動作。
由于我想從應用程式中的幾乎每個控制器呼叫此函式,因此將其放入Application.Helper.Controller似乎是正確的選擇,但我無法弄清楚型別簽名。
函式如下,在普通控制器中是這樣作業的:
ensureIsSubscribed :: _ => IO ()
ensureIsSubscribed = do
case currentUserOrNothing of
Just loggedInUser -> do
case (get #subscriptionId loggedInUser) of
Nothing -> redirectToPath "/NewCheckoutSession"
_ -> redirectToPath "/Welcome"
Nothing -> pure ()
但是當把上面的Application.Helper.Controller我得到這個錯誤:
? Could not deduce (HasField
"subscriptionId" CurrentUserRecord (Maybe a0))
arising from a use of ‘get’
from the context: ?context::ControllerContext
bound by the inferred type of
ensureIsSubscribed :: (?context::ControllerContext) => IO ()
at Application/Helper/Controller.hs:(8,1)-(14,26)
The type variable ‘a0’ is ambiguous
? In the expression: (get #subscriptionId loggedInUser)
In a stmt of a 'do' block:
case (get #subscriptionId loggedInUser) of
Nothing -> redirectToPath "/NewCheckoutSession"
_ -> redirectToPath "/Welcome"
In the expression:
do case (get #subscriptionId loggedInUser) of
Nothing -> redirectToPath "/NewCheckoutSession"
_ -> redirectToPath "/Welcome"
|
11 | case (get #subscriptionId loggedInUser) of
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
題
為什么以上不起作用,正確的型別簽名是什么(如果這是問題)?
uj5u.com熱心網友回復:
該CurrentUserRecord定義在Web/Types.hs如type instance CurrentUserRecord = User。如果Web.Types模塊未匯入,則型別CurrentUserRecord不能用它的定義替換User。
因此,修復方法是import Web.Types在您的Application.Helper.Controller模塊中添加一個:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/316849.html
上一篇:RankNTypes函式上的映射
