使用方尖碑路由时,RoutedT被解释为T.Text

时间:2020-05-16 18:05:27

标签: haskell reflex obelisk

我正在尝试使用Obelisk构建基本的Web应用程序,并且一直在遵循本教程:https://github.com/obsidiansystems/obelisk/pull/733/files#diff-fe2dadb00af0583d3841e501760804eaR457

我不明白为什么当我的Obelisk安装是默认的Obelisk项目时,如果仅对上面的教程进行修改以实现“配置文件”和“事件” /“投入工作”页面,为什么会出现此类型错误。 >

我的最终目标是通过我在下面发布的Frontend.hs中定义的两个按钮(例如elAttr "p" ("class" =: "button is-link is-large is-fullwidth") $ text "My Profile")来连接这些路由/链接。

这是我得到的错误:

frontend/src/Frontend.hs:41:41-60: error:
    • Couldn't match type ‘T.Text’ with ‘RoutedT t EventId m ()’
      Expected type: RoutedT t a m ()
        Actual type: T.Text
    • In the second argument of ‘(<>)’, namely ‘T.pack (show uidVal)’
      In the expression: text "This event : " <> T.pack (show uidVal)
      In the second argument of ‘($)’, namely
        ‘\ (EventId uidVal)
           -> text "This event : " <> T.pack (show uidVal)’
    • Relevant bindings include
        dUserId :: Dynamic t EventId
          (bound at frontend/src/Frontend.hs:39:13)
   |
41 |                 text "This event : " <> T.pack (show uidVal)
   |                                         ^^^^^^^^^^^^^^^^^^^^
frontend/src/Frontend.hs:48:40-59: error:
    • Couldn't match type ‘T.Text’ with ‘RoutedT t UserId m ()’
      Expected type: RoutedT t a m ()
        Actual type: T.Text
    • In the second argument of ‘(<>)’, namely ‘T.pack (show uidVal)’
      In the expression: text "This user : " <> T.pack (show uidVal)
      In the second argument of ‘($)’, namely
        ‘\ (UserId uidVal) -> text "This user : " <> T.pack (show uidVal)’
    • Relevant bindings include
        dUserId :: Dynamic t UserId
          (bound at frontend/src/Frontend.hs:46:13)
   |
48 |                 text "This user : " <> T.pack (show uidVal)
   |                                        ^^^^^^^^^^^^^^^^^^^^

这是我的Common.Route文件的摘录,这是本教程中仅有的一半实现的部分……所以也许是问题所在?

data FrontendRoute :: * -> * where
  FrontendRoute_Main :: FrontendRoute ()
  -- This type is used to define frontend routes, i.e. ones for which the backend will serve the frontend.
  FrontendRoute_MyProfile :: FrontendRoute UserId
  FrontendRoute_PutInWork :: FrontendRoute EventId

fullRouteEncoder
  :: Encoder (Either Text) Identity (R (FullRoute BackendRoute FrontendRoute)) PageName
fullRouteEncoder = mkFullRouteEncoder
  (FullRoute_Backend BackendRoute_Missing :/ ())
  (\case
      BackendRoute_Missing -> PathSegment "missing" $ unitEncoder mempty)
  (\case
      FrontendRoute_Main -> PathEnd $ unitEncoder mempty
      -- FrontendRoute_MyProfile -> PathSegment "user" _todo
      -- FrontendRoute_PutInWork -> PathSegment "event" _todo
      )

这也是Frontend.hs的主要功能:

frontend :: Frontend (R FrontendRoute)
frontend = Frontend
  { _frontend_head = do
      el "title" $ text "The App Name"
      elAttr "link" ("href" =: static @"bulma.css" <> "type" =: "text/css" <> "rel" =: "stylesheet") blank
  , _frontend_body = subRoute_ $ \case          -- https://github.com/obsidiansystems/obelisk/pull/733/files#diff-fe2dadb00af0583d3841e501760804eaR425
        FrontendRoute_PutInWork -> do
            text "We're on the Put In Work page! But which event?"
            dUserId <- askRoute
            dyn_ $ ffor dUserId $ \(EventId uidVal) ->
                text "This event : " <> T.pack (show uidVal)
            routeLink (FrontendRoute_Main :/ ()) $
                text "To the main page!"
        FrontendRoute_MyProfile -> do
            text "We're on the My Profile page! But which user?"
            dUserId <- askRoute
            dyn_ $ ffor dUserId $ \(UserId uidVal) ->
                text "This user : " <> T.pack (show uidVal)
            routeLink (FrontendRoute_Main :/ ()) $
                text "To the main page!"
        FrontendRoute_Main -> do
            elClass "div" "content" $ do
            elAttr "h1" ("class" =: "content") $ text "Welcome to the app!"
            el "h2" $ text $ T.pack commonStuff
            elAttr "p" ("class" =: "button is-primary is-large is-fullwidth") $ text "Put In Work"
            elAttr "p" ("class" =: "button is-link is-large is-fullwidth") $ text "My Profile"

            elAttr "p" ("class" =: "box") $ text "News about the app and the world (box is color-coded based on content type), special offers from partners, prompts that there is a new partner nearby, and prompts to push yourself harder eventually.  At launch, it's just a welcome text / helper note."

            routeLink (FrontendRoute_MyProfile :/ User 42) $
                text "Visit the page of User # 42"
    return ()

}

1 个答案:

答案 0 :(得分:1)

这是我在编写Reflex应用程序时也会犯的一个常见错误。

解决方案是使用$。为什么?让我们看一下fixity declarations

有三种固定性,分别是非关联性,左关联性和右关联性(分别为infix,infixl和infixr),以及10个优先级,包括0到9(包括0到9)(0绑定最少,而9绑定)最紧密地)。

参加GHCi代表赛,

λ> :i (<>)
class Semigroup a where
  (<>) :: a -> a -> a
  ...
        -- Defined in ‘GHC.Base’
infixr 6 <>
λ> :i ($)
($) :: (a -> b) -> a -> b       -- Defined in ‘GHC.Base’
infixr 0 $
λ>

GHC手册说:“ 普通构造函数应用程序的优先级高于中缀构造函数应用程序的优先级(因此,:Foo a解析为:(Foo a))。”,因此在您的内容中:

text "This event : " <> T.pack (show uidVal)

它解释为

(text "This event : ") <> T.pack (show uidVal)

这显然是错误的。您可以通过显式定义表达式的范围来解决此问题:

text ("This event : " <> T.pack (show uidVal))

或者,只需使用上述$运算符(在反射代码库中通常首选)即可:

text $ "This event : " <> T.pack (show uidVal)