尝试在Elm中进行结构化类型时类型不匹配?

时间:2017-04-02 15:50:58

标签: elm type-mismatch

我有一个类型:

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

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

的列表
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似乎很匹配。

1 个答案:

答案 0 :(得分:4)

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

不同
renderItem { attributes, children } =

后者可以按照您的方式进行模式匹配:

renderItem (MenuItem { attributes, children }) =

但前者,因为它包含在数据构造函数中也需要解包:

function getPendingOrdersForUser($username, $connection)
  {
    $ordersQuery = "SELECT * FROM orders at WHERE at.username=:username;";
    $stmt = $connection->prepare($ordersQuery);
    $stmt->execute(array(':username' => $username));
    $result = !!$stmt->fetchAll(PDO::FETCH_ASSOC);
    return $result;
  }