2016-07-24 75 views
1

这与Elm教程(http://guide.elm-lang.org/architecture/effects/random.html)有关,我试图为其中一个挑战产生一个随机数列表(目前只有2个项目)。当在Elm中生成随机数列表时产生类型冲突

The 2nd argument to function `generate` is causing a mismatch. 

39|    Random.generate NewFaces intList) 
             ^^^^^^^ 
Function `generate` is expecting the 2nd argument to be: 

    Random.Generator List 

But it is: 

    Random.Generator (List Int) 

这是我使用的代码:

试图生成列表时,我得到一个类型错误

update : Msg -> Model -> (Model, Cmd Msg) 
update msg model = 
    case msg of 
    Roll -> 
     let 
     intList : Random.Generator (List Int) 
     intList = 
      Random.list 2 (Random.int 1 6) 
     in 
     (model, Random.generate NewFaces intList) 
    NewFaces newFaces -> 
     ({ model | dieFaces = newFaces}, Cmd.none) 

我仍然在试图让我的头缠着类型 - - 特别是关于名单。我猜(List Int)意味着一个整数列表,但我不确定List本身是什么意思(任意类型的列表?)。

我已经发挥了代码,将发生器拉出到一个单独的变量(intList),并明确地键入它。我也试着输入Random.Generator List,这也引发了一个错误。基本上,我可以使用帮助搞清楚如何协调List与(List Int)。

谢谢 - 超新的榆树,所以任何指导表示赞赏。

回答

2

基于错误信息,它看起来像你可能已经定义NewFaces这样的:

type Msg 
    = Roll 
    | NewFaces List 

List需要一个单一类型的参数,所以它应该被定义为

type Msg 
    = Roll 
    | NewFaces (List Int) 
+0

哦!好吧,所以它预期的原因列表是因为我说NewFaces应该期望List而不是(List Int) - 这是有道理的,谢谢!我非常关注Random.generate函数,甚至没有考虑NewFaces消息。 –