2017-04-02 28 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 }) =