I have a model that looks like this:
type alias Model =
{ ...
, dripText : String
, simulationHoursText : String
, simulationMinutesText : String
...
}
Those strings each correspond to a text field containing floating point
numbers. The whole thing looks like this:
The text fields ensure that the values are all valid when characters are typed,
using `onInput` messages. I would very much like to write something like:
— Ignore the fact that this `update` doesn’t return
— a `Cmd Msg`. That’s coming.
update : Msg -> Model -> Model
update msg model =
case msg of
ChangedDripText string ->
updateWhen isValidFloatString dripText string
ChangedHoursText string ->
updateWhen isValidIntString simulationHoursText string
ChangedMinutesText string ->
updateWhen isValidIntString simulationMinutesText string
… because that shows what’s different about each case. (Especially useful is
highlighting which fields accept integers and which floats.)
However, I think - based on
https://lexi-lambda.github.io/blog/2015/11/06/functionally-updating-record-types-in-elm/
- there’s no way to do that. Which has me writing this copypasta:
updateNextSpeed model nextString =
if isValidFloatString nextString then
{model | dripText = nextString }
else
model
updateHours model nextString =
if isValidIntString nextString then
{model | simulationHoursText = nextString }
else
model
updateMinutes model nextString =
if isValidIntString nextString then
{model | simulationMinutesText = nextString }
else
model
update : Msg -> Model -> Model
update msg model =
case msg of
ChangedDripText string ->
updateNextSpeed model string
ChangedHoursText string ->
updateHours model string
ChangedMinutesText string ->
updateMinutes model string
Is there a better way to handle this?
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.