Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elm how to update model based on input type number

I have an input like this :

input [ type' "number", onInput NewValue ] [ text <| toString model.value ]

How to update the model ? I have something like this:

NewValue nb ->
      ( { model | value = nb }, Cmd.none )

I don't know if in input type number the value is an Int or a String. I also try this:

NewValue nb ->
  let
    nb = Result.withDefault 0 (String.toInt nb)
  in
    ( { model | value = nb }, Cmd.none )

WIth the second version I got this error:

The return type of function `withDefault` is being used in unexpected ways.

44|         nb = Result.withDefault 0 (String.toInt nb)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The function results in this type of value:

    Int

Which is fine, but the surrounding context wants it to be:

    String
like image 749
BoumTAC Avatar asked Jun 11 '16 23:06

BoumTAC


1 Answers

Change the function name nb to something else as it is already assigned as a String and you can't overwrite it.

NewValue nb ->
  let
    newInt = Result.withDefault 0 (String.toInt nb)
  in
    ( { model | value = newInt }, Cmd.none )
like image 83
farmio Avatar answered Sep 30 '22 21:09

farmio