2017-06-21 77 views
1

我想写一个函数,返回Elm中的两个列表,但我遇到了问题。看起来编译器不能匹配空列表[]的类型。从函数返回列表的元组

import Html exposing (text) 

main = 
    let 
    (a, b) = genList 
    in 
    text "Hello" 


genList: List Float List Float 
genList = 
    ([], []) 

的编译器错误如下:

Detected errors in 1 module. 


-- TYPE MISMATCH --------------------------------------------------------------- 

`genList` is being used in an unexpected way. 

6|  (a, b) = genList 
       ^^^^^^^ 
Based on its definition, `genList` has this type: 

    List Float List Float 

But you are trying to use it as: 

    (a, b) 


-- TYPE MISMATCH --------------------------------------------------------------- 

The definition of `genList` does not match its type annotation. 

11| genList: List Float List Float 
12| genList = 
13| ([], []) 

The type annotation for `genList` says it is a: 

    List Float List Float 

But the definition (shown above) is a: 

    (List a, List b) 

我还没有发现给人一种类型提示为空列表中的任何方式。检查的文件,它不会去深: https://guide.elm-lang.org/core_language.html http://elm-lang.org/docs/syntax#functions

回答

3

类型签名也需要(.., ..)元组的语法,如:

genList: (List Float, List Float) 
genList = 
    ([], []) 

[]是,虽然产生一个空列表正确的语法。如果您想了解List类型的更多信息,最好查看package.elm-lang.org上的文档。您分享的两个链接比综合性文档更“介绍指南”。