【问题标题】:Type error in the update function in Elm在 Elm 的更新函数中输入错误
【发布时间】:2016-05-16 08:17:40
【问题描述】:

我是 elm (0.17) 的新手,我试图了解它是如何工作的。在这种情况下,我尝试开发一种项目估算。 这就是我所做的:

import Html exposing (..)
import Html.App as Html
import Html.Attributes exposing (..)

import Html.Events exposing (onClick)

main =
  Html.program
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }

-- model
type alias Host = {
  name : String,
  cost : Int
}
type alias Model =
  { email : String
  , hosting : List Host
  , period : List Int
  , interventionDays : List Int
  , total : Int
  }

init : (Model, Cmd Msg)
init =
  (Model "init@email.fr" [{name="AAA", cost=15}, {name="BBB", cost=56}, {name="CCC", cost=172}] [1..12] [1..31] 0, Cmd.none)


type Msg = Submit | Reset

calculate : Int
calculate = 42 -- to test

update : Msg -> Model -> (Model, Cmd Msg)
update action model =
  case action of
    Submit ->
      (model, calculate)
    Reset ->
      (model, Cmd.none)

-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
  Sub.none


-- view
hostOption host =
  option [ value (toString host.cost) ] [ text host.name ]

durationOption duration =
  option [value (toString duration) ] [ text (toString duration)]

view : Model -> Html Msg
view model =
  Html.form []
    [ h2 [] [ text "Estimate your project"]
    , input [ placeholder model.email ] []
    , select []
      (List.map hostOption model.hosting)
    , select []
      (List.map durationOption model.period)
    , select []
      (List.map durationOption model.interventionDays)
    , Html.span [][text (toString model.total)]
    , button [onClick Submit] [text "Submit"]
    , button [onClick Reset] [text "Reset"]
    ]

我想我已经理解了 elm 背后的一些想法,但我需要帮助,因为 elm-make 命令返回:

The 1st and 2nd branches of this `case` produce different types of values.

40|   case action of
41|     Submit ->
42|       (model, calculate)
43|     Reset ->
44|>      (model, Cmd.none)

The 1st branch has this type:

    ( a, Int )

But the 2nd is:

    ( a, Cmd a )

Hint: All branches in a `case` must have the same type. So no matter which one
we take, we always get back the same type of value.

Detected errors in 1 module.                                        

我了解这个问题,但我不知道如何解决它。我是否必须定义我的计算函数才能处理模型数据?

谢谢

【问题讨论】:

    标签: elm


    【解决方案1】:

    我猜你想用calculate 更新模型的total 字段。

    update 函数返回的第一个元组项是更新后的模型。就目前情况而言,您的两个操作都会返回现有模型而不更改它。所以你可以试试这个:

    case action of
      Submit ->
        ({ model | total = calculate }, Cmd.none)
      Reset ->
       init
    

    有关更新记录的语法,请参阅here

    请注意,我还更改了 Reset 分支以返回 init,即初始模型和命令。

    【讨论】:

      【解决方案2】:

      编译器错误告诉你更新方法,在某些情况下会返回一个(Model, Cmd)元组,而在另一些情况下会返回一个(Model, Int)元组。

      您拥有的更新函数应该返回修改后的模型以及一个执行操作的 Cmd,换句话说,一个 (Model, Cmd) 元组。

      如果您返回(model, calculate),它将返回一个(Model, Int) 元组,因为calculate 是一个Int。这就是破坏编译的原因。

      所以要修复它,首先你需要决定如何处理每个 Msg.我假设他们的名字Calculate Msg 将更新总数,Reset Msg 将模型设置为默认状态。

      你可以这样做:

          case action of
             Submit ->
                ({ model | total = calculate }, Cmd.none)
             Reset ->
                init
      

      在这种情况下,两个分支都将返回一个 (Model, Cmd) 类型的元组。

      注意Reset 分支将返回init,它已经是(Model, Cmd) 类型。

      查看官方指南了解更多示例:http://guide.elm-lang.org/index.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-11
        • 1970-01-01
        • 2015-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-22
        相关资源
        最近更新 更多