Search code examples
haskellreflexobelisk

RoutedT interpreted as T.Text when using Obelisk routes


I am trying to build a basic web app with Obelisk and have been following this tutorial: https://github.com/obsidiansystems/obelisk/pull/733/files#diff-fe2dadb00af0583d3841e501760804eaR457

I do not understand why I would get this type error when my Obelisk installation is a default Obelisk project with only the modifications from the tutorial above to implement "Profile" and "Event"/"Put In Work" pages.

My ultimate goal is that these routes/links are connected via the two buttons defined in my Frontend.hs that I have posted below (e.g. elAttr "p" ("class" =: "button is-link is-large is-fullwidth") $ text "My Profile").

This is the error I'm getting:

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)
   |                                        ^^^^^^^^^^^^^^^^^^^^

This is an excerpt from my Common.Route file, which is the only half-implemented part of the tutorial ... so maybe this is the problem?

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
      )

Here is the primary function from Frontend.hs as well:

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 ()

}

Solution

  • This is a common mistake I also make when writing Reflex apps.

    The solution is to use $. Why? Let's look at the fixity declarations,

    There are three kinds of fixity, non-, left- and right-associativity (infix, infixl, and infixr, respectively), and ten precedence levels, 0 to 9 inclusive (level 0 binds least tightly, and level 9 binds most tightly).

    by playing in GHCi repl,

    λ> :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 $
    λ>
    

    The GHC manual says "normal constructor application has higher precedence than infix constructor application (thus a : Foo a parses as a : (Foo a)).", therefore in your:

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

    it is interpreted as

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

    which is obviously wrong. You can fix this by explicitly scoping the expression:

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

    Or, simply by using the aforementioned $ operator (which is generally preferred in reflex code base):

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