2017-04-02 80 views
0

我有一个类型:尝试在Elm中解构类型时类型不匹配?

type MenuItem msg 
    = MenuItem 
     { attributes : List (Attribute msg) 
     , children : List (Html msg) 
     } 

这是一个NavBar的一部分。然后,我有一个函数renderItems呈现的MenuItem个清单:

renderItems : List (MenuItem msg) -> Html msg 
renderItems items = 
    ul [ class "nav-list" ] (List.map renderItem items) 

renderItems,你可以看到,来电renderItem,它看起来像这样:

renderItem : MenuItem msg -> Html msg 
renderItem { attributes, children } = 
    li [ class "nav-item" ] 
     [ a ([ class "nav-link" ] ++ attributes) 
      children 
     ] 

但我在这里得到一个编译错误:

This record is causing problems in this pattern match. 

14| renderItem { attributes, children } = 
       ^^^^^^^^^^^^^^^^^^^^^^^^ 
The pattern matches things of type: 

    MenuItem msg 

But the values it will actually be trying to match are: 

    { c | attributes : a, children : b } 

Detected errors in 1 module. 

任何人都可以为我解释这个吗?我不明白这个错配。 attributeschildren似乎相当不错。

回答

4
type MenuItem msg 
    = MenuItem 
     { attributes : List (Attribute msg) 
     , children : List (Html msg) 
     } 

type alias MenuItem msg 
    = { attributes : List (Attribute msg) 
     , children : List (Html msg) 
     } 

后者明显可以模式匹配你的方式:

renderItem { attributes, children } = 

但前者,因为它被包裹在一个数据构造还需要将被打开:

renderItem (MenuItem { attributes, children }) =