2016-06-11 46 views
3

模式,我有一个这样的输入:榆树如何更新基于输入型号

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

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

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

我不知道,如果在输入类型号的值是一个IntString。 我也试试这个:

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

与第二个版本,我得到这个错误:

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 
+0

你的第二次尝试看起来不错。什么不工作? – farmio

+0

我添加了错误 – BoumTAC

回答

9

改变函数名nb设置为别的,因为它已经被指定为一个字符串,你不能覆盖它。

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