I use Elm 0.18.
I have written the following, simple Elm program. It is supposed to
accumulate values, sent from JS. I'd also like to get the accumulated
value from the program, back to JS.
port module App exposing (..)
import Json.Decode
import Task
-- model
type alias Model =
{ acc : Int
}
addNumber : Int -> Model -> Model
addNumber n model =
{ model | acc = model.acc + n }
init : (Model, Cmd Msg)
init =
(Model 0, Cmd.none)
-- update
type Msg
= GetAcc ()
| AddNumber Int
port toJS : Int -> Cmd msg
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
GetAcc ->
(model, toJS model.acc)
AddNumber n ->
(addNumber n model, Task.perform GetAcc <| Task.succeed ())
-- subscriptions
port fromJS : (Int -> msg) -> Sub msg
subscriptions : Model -> Sub Msg
subscriptions model =
fromJS AddNumber
-- main
main =
Platform.program { init = init,
update = update,
subscriptions = subscriptions }
There are two problems with the code:
1) Without the `import Json.Decode`, the code is compiled, yet I get
the `ReferenceError: _elm_lang$core$Json_Decode$int is not defined`
error after loading the result JS file,
It's similar to this issue: https://github.com/elm-lang/core/issues/683
Is it an Elm bug?
2) The message type is defined as follows:
type Msg
= GetAcc ()
| AddNumber Int
With such a definition, I can create a command to get the accumulator
value using the `Task` module functions:
Task.process GetAcc <| Task.succeed ()
Is it possible to create command for the following Msg definition:
type Msg
= GetAcc
| AddNumber Int
In this case, `GetAcc` has a type of `Msg`, while in my code, its type
is `() -> Msg`, which was needed for the function `Task.process`.
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.