1

I'm new to elm (0.17) and I try to understand how it works. In this case, I try to develop a kind of project estimation. This is what I did:

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"]
    ]

I think I have understood some ideas behind elm but I need help because elm-make command returns:

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.                                        

I understand the problem but I do not know how to fix it. Do I have to define my calculate function to work with model data ?

Thanks

Abdulrahman Alsoghayer
  • 16,462
  • 7
  • 51
  • 56
billyJoe
  • 2,034
  • 2
  • 24
  • 37

2 Answers2

3

I'm going to guess that you want to update the the total field of your model with calculate.

The first tuple item that the update function returns is the updated model. As things stand, both of your actions return the existing model without changing it. So you could try this:

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

See here for the syntax for updating records.

Note that I also changed the Reset branch to return init, the initial model and command.

TheQuickBrownFox
  • 10,544
  • 1
  • 22
  • 35
2

The compiler error is telling you that the update method, in some cases will return a (Model, Cmd) tuple, and in another cases will return a (Model, Int) tuple.

The update function as you have it, should return the modified model and also a Cmd to execute an action, in other words, a (Model, Cmd) tuple.

If you return (model, calculate) it will return a (Model, Int) tuple, since calculate is an Int. That is what is breaking the compiling.

So to fix it, first you need to decide what to do with each of the Msg. I assume by the name of them that the Calculate Msg will update the total and the Reset Msg will set the model to the default state.

For that you could do:

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

In this case, both branches will return a tuple of type (Model, Cmd).

Note that the Reset branch will return init, which is already of type (Model, Cmd).

Check the official guide for more examples: http://guide.elm-lang.org/index.html

afcastano
  • 548
  • 3
  • 17