Elm Tagged Connection Type Comparison Constructor

Is it possible to use the ==constructor of tagged types of joins in Elm for comparison, or do you need to use a case?

Example:

  type alias Model =
    { accessToken : String
    , page : Page
    , config : Config 
    } 

  type Page 
    = Home String
    | Profile String


   menu : Model -> Html Msg
   menu model = 
      div []
          [ a [ href "#home", classList [ ("active", model.page == Home) ] ][ text "Home" ]
          , a [ href "#profile", classList [ ("active", model.page == Profile)] ][ text "Profile" ]        
          ]

In this example, I would like to write something like model.page == Home to check if the current page is home, so that I can set the css class to “active” from this link, but it seems to me that I should use for this is a case that I can do, but kindly diligently implement this situation.

+6
source share
1 answer

, ==, , . :

isHome : Page -> Bool
isHome pg =
    case pg of
        Home _ -> True
        _ -> False

isProfile : Page -> Bool
isProfile pg =
    case pg of
        Profile _ -> True
        _ -> False

:

menu : Model -> Html Msg
menu model =
    div []
        [ a [ href "#home", classList [ ( "active", isHome model.page ) ] ] [ text "Home" ]
        , a [ href "#profile", classList [ ( "active", isProfile model.page ) ] ] [ text "Profile" ]
        ]
+7

All Articles