Hi!
I’m a little bit stuck and would appreciate some help. I’m writing an Elm application which has a somewhat large model, part of which is optional (it holds some configuration values). Basically it looks like this:
type alias Config =
{ color : String }
type alias MyModel =
{ some : Bool
, other : String
, config : Maybe Config
}
My view function waits until the configuration field appears before doing much of anything:
view obj =
case obj.config of
Nothing ->
Html.text "No config"
Just cfg ->
Html.text cfg.color
So far so good. However, as the application has been growing, the view function has also grown and now consists of dozens of helper functions. Whenever these functions need to use a config value, they need to figure out if there is a config or not:
view obj =
case obj.config of
Nothing ->
Html.text "Waiting for config"
Just cfg ->
Html.text ("The color is " ++ color obj)
color obj =
case obj.config of
Nothing ->
""
Just cfg ->
cfg.color
This doesn’t make a whole lot of sense since I already know that I won’t be calling this function unless I have a non-Nothing configuration.
So what I’ve done is add an additional parameter to all functions that need a config:
view obj =
case obj.config of
Nothing ->
Html.text "Waiting for config"
Just cfg ->
Html.text ("The color is " ++ color obj cfg)
color obj cfg =
cfg.color
This works fine, but it seems a bit redundant to me – the config value gets passed twice, once as the Maybe argument in obj
and once explicitly. So I’ve tried to come up with a solution that uses type aliases like this:
type alias Configured a =
{ a | config : Config }
But from what I can tell I now need to manually convert my MyModel
object to a Configured MyModel
object every time view
is called which seems a bit silly.
Is there a more elegant solution for this? Something like a way to say “Use this record, but add a config value to it”? Or am I doing something fundamentally wrong?