3

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

pietro909
  • 1,811
  • 1
  • 19
  • 26

1 Answers1

7

Random.pair generates a tuple, so your NewFace message must accept a tuple as a parameter. Try changing it to this:

type Msg
  = Roll
  | NewFace (Int, Int)
Chad Gilbert
  • 36,115
  • 4
  • 89
  • 97