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

标签 random command messages elm

我正在学习 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)))

我确信我遗漏了一些微不足道的东西,尽管这是我在 Elm 的第一天并且越来越具有挑战性。

谢谢

最佳答案

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

type Msg
  = Roll
  | NewFace (Int, Int)

关于random - 在 Elm 中生成带有多个参数的消息的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37618500/

相关文章:

linux - 在 Linux 中同步目录内容

php - 在 php 脚本仍在执行时回显消息

random - 随机生成的 2D 点是否会聚集在一起,如何阻止它?

random - SPSS 从受访者的输入中随机选择一个数字

linux - Find 命令在结果前面打印数字

ruby-on-rails - Ruby on Rails - Lynda - 生成 Controller 和 View , 'Rails Generate' 命令出错

c++ - std::random_device 在 g++ 中不工作

java - 随机加权选择然后去除权重

objective-c - iPhone SDK Objective C 是否支持函数内的函数?

ios - 使用自动布局的 UITextView 动态高度 - 每行新行的文本截断顶行