Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 326
pietro909 Avatar asked Dec 08 '25 14:12

pietro909


1 Answers

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)
like image 90
Chad Gilbert Avatar answered Dec 11 '25 02:12

Chad Gilbert