I’m drawing my sidebar links like this:
[ viewSidebarLink "/" "Dashboard" "fa-house" (model.page == Dashboard)
, viewSidebarLink "/my-forms" "My Forms" "fa-list" (model.page == MyForms)
, viewSidebarLink "/settings" "Settings" "fa-gear" (model.page == Settings)
]
-- ...
viewSidebarLink : String -> String -> String -> Bool -> Html.Html Msg
viewSidebarLink linkTarget linkText icon active =
...
This works as long as none of my pages take any extra data:
type Page
= Dashboard
| MyForms
| Settings
| Error404
However, I want to flesh out my pages, adding extra state for the dashboard, for example:
type Page
= Dashboard DashboardState
| MyForms
| Settings
| Error404
Now the code for drawing my sidebar links breaks, because I can no longer do (model.page == Dashboard)
, instead I have to do this awful, ugly, verbose monstrosity:
[ viewSidebarLink "/"
"Dashboard"
"fa-house"
(case model.page of
Dashboard _ ->
True
_ ->
False
)
, viewSidebarLink "/my-forms" "My Forms" "fa-list" (model.page == MyForms)
, viewSidebarLink "/settings" "Settings" "fa-gear" (model.page == Settings)
]
Does Elm have any way to check if an expression matches a pattern without doing a full blown case expression?
It seems extremely verbose to use a case expression for this use case, it seems like there should be a shortcut for this, such as (case model.page of Dashboard _)
.
Or am I using a bad pattern here, is there a better way to achieve the same result? (i.e. check which “branch” of the state I’m in, ignoring any data underneath)