Elm中的更新函数中存在类型错误

Elm中的更新函数中存在类型错误,elm,Elm,我是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 , up

我是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会返回命令:

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.                                        
我理解这个问题,但不知道如何解决。我是否必须定义计算函数才能处理模型数据


谢谢

我猜您想用
计算
更新模型的
总计
字段

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

case action of
  Submit ->
    ({ model | total = calculate }, Cmd.none)
  Reset ->
   init
有关更新记录的语法,请参阅


请注意,我还将重置分支更改为返回初始模型和命令
init

我猜您想用
计算
更新模型的
总计
字段

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

case action of
  Submit ->
    ({ model | total = calculate }, Cmd.none)
  Reset ->
   init
有关更新记录的语法,请参阅


请注意,我还将重置分支更改为返回初始模型和命令
init

编译器错误告诉您,更新方法在某些情况下将返回
(model,Cmd)
元组,在另一些情况下将返回
(model,Int)
元组

update函数应该返回修改后的模型以及执行操作的Cmd,换句话说,就是
(model,Cmd)
元组

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

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

为此,您可以:

    case action of
       Submit ->
          ({ model | total = calculate }, Cmd.none)
       Reset ->
          init
在这种情况下,两个分支都将返回类型为
(Model,Cmd)
的元组

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


查看官方指南了解更多示例:

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

update函数应该返回修改后的模型以及执行操作的Cmd,换句话说,就是
(model,Cmd)
元组

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

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

为此,您可以:

    case action of
       Submit ->
          ({ model | total = calculate }, Cmd.none)
       Reset ->
          init
在这种情况下,两个分支都将返回类型为
(Model,Cmd)
的元组

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

查看官方指南了解更多示例: