在Elm中使用带有多个参数的消息生成命令

时间:2016-06-03 15:41:25

标签: random command messages elm

我正在关注榆树教程Random,我一直试图一起跑两个骰子。

我修改了邮件以提供两个数字:

type Msg
    = Roll
    | NewFace Int Int

然后我需要生成在更新函数中发送消息的命令:

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

问题在于,使用此构造失败:

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

首先,我尝试用括号分组最后一个参数:

-- 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)))

然后我发现有一个Random.pair函数:

-- 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)))

我确信我错过了一些微不足道的事情,虽然这是我与榆树的第一天,并且正在变得具有挑战性。

由于

1 个答案:

答案 0 :(得分:8)

Random.pair会生成一个元组,因此您的NewFace消息必须接受元组作为参数。尝试将其更改为:

type Msg
  = Roll
  | NewFace (Int, Int)