elm如何根据输入类型编号更新模型

Bou*_*TAC 5 int numbers input elm

我有这样的输入:

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

如何更新模型?我有这样的事情:

NewValue nb ->
      ( { model | value = nb }, Cmd.none )
Run Code Online (Sandbox Code Playgroud)

我不知道输入类型编号中的值是a Int还是a String.我也试试这个:

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

对于第二个版本,我收到此错误:

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
Run Code Online (Sandbox Code Playgroud)

far*_*mio 10

将函数名nb更改为其他名称,因为它已被指定为String,您无法覆盖它.

NewValue nb ->
  let
    newInt = Result.withDefault 0 (String.toInt nb)
  in
    ( { model | value = newInt }, Cmd.none )
Run Code Online (Sandbox Code Playgroud)