vendredi 3 juin 2016

Generating a command with a message with more than one arguments in Elm

I'm following the Elm tutorial Random and I got stuck trying to run two dice together.

I modified the message to deliver two numbers:

type Msg
    = Roll
    | NewFace Int Int

then I need to generate the command which sends the message in the update function:

(model, Random.generate NewFace (Random.int 1 6))

the problem is that with this construct it fails:

-- error: Function `generate` is expecting 2 arguments, but was given 3.
(model, Random.generate NewFace (Random.int 1 6) (Random.int 1 6))

At first I tried grouping the last argument with parenthesis:

-- same error as before plus: 
-- The type annotation is saying:
--     Msg -> Model -> ( Model, Cmd Msg )
-- But I am inferring that the definition has this type:
--     Msg -> Model -> ( Model, Cmd (Int -> Msg) )
(model, Random.generate NewFace ((Random.int 1 6) (Random.int 1 6)))

Then I found that there is a Random.pair function:

-- still complaining about update's signature and moreover
-- Function `generate` is expecting the 2nd argument to be:
--    Random.Generator Int
-- But it is:
--    Random.Generator ( Int, Int )

(model, Random.generate NewFace (Random.pair (Random.int 1 6) (Random.int 1 6)))

I'm sure I'm missing something trivial, though is my first day with Elm and is getting challenging.

Thanks




Aucun commentaire:

Enregistrer un commentaire