错误无法匹配类型
Error could not match type
我有一个类型:
newtype User = User
{ id :: String
, email :: String
, last_update :: String
}
和一个函数:
import Pux.DOM.HTML (HTML)
import Pux.DOM.HTML.Attributes (key)
import Text.Smolder.HTML as H
import Text.Smolder.HTML.Attributes as A
userRow :: User -> HTML Event
userRow user =
H.tr ! key user.id ! A.className "user-item" $ do
H.td ! key ("email") $ text user.email
H.td ! key ("last_update") $ text user.last_update
H.td ! key ("actions") ! A.className "actions" $ do
H.a ! key ("delete") ! A.className "action-delete" #! onClick (pure $ OpenDeleteModal (show user.id)) $ do
H.span
! A.className "dashicons dashicons-trash"
! A.alt "Delete"
! A.title "Delete"
$ text ""
但是类型不会统一:
268 H.tr ! key user.id ! A.className "user-item" $ do
^^^^
Could not match type
{ id :: String
| t0
}
with type
User
while checking that type User
is at least as general as type { id :: String
| t0
}
while checking that expression user
has type { id :: String
| t0
}
while checking type of property accessor user.id
in value declaration userRow
where t0 is an unknown type
我看不出我做错了什么。在我看来,用户类型有一个 "id" 成员,它是一个字符串,因此至少与
一样通用
type { id :: String
| t0
}
User
是围绕一条记录的新类型,因此它将记录包裹起来。为了访问记录字段,您首先需要将其解包。在您的情况下执行此操作的最简单方法是在 user
参数上进行模式匹配:
userRow :: User -> HTML Event
userRow (User user) =
H.tr ! key user.id ! A.className "user-item" $ do
...
我有一个类型:
newtype User = User
{ id :: String
, email :: String
, last_update :: String
}
和一个函数:
import Pux.DOM.HTML (HTML)
import Pux.DOM.HTML.Attributes (key)
import Text.Smolder.HTML as H
import Text.Smolder.HTML.Attributes as A
userRow :: User -> HTML Event
userRow user =
H.tr ! key user.id ! A.className "user-item" $ do
H.td ! key ("email") $ text user.email
H.td ! key ("last_update") $ text user.last_update
H.td ! key ("actions") ! A.className "actions" $ do
H.a ! key ("delete") ! A.className "action-delete" #! onClick (pure $ OpenDeleteModal (show user.id)) $ do
H.span
! A.className "dashicons dashicons-trash"
! A.alt "Delete"
! A.title "Delete"
$ text ""
但是类型不会统一:
268 H.tr ! key user.id ! A.className "user-item" $ do
^^^^
Could not match type
{ id :: String
| t0
}
with type
User
while checking that type User
is at least as general as type { id :: String
| t0
}
while checking that expression user
has type { id :: String
| t0
}
while checking type of property accessor user.id
in value declaration userRow
where t0 is an unknown type
我看不出我做错了什么。在我看来,用户类型有一个 "id" 成员,它是一个字符串,因此至少与
一样通用type { id :: String
| t0
}
User
是围绕一条记录的新类型,因此它将记录包裹起来。为了访问记录字段,您首先需要将其解包。在您的情况下执行此操作的最简单方法是在 user
参数上进行模式匹配:
userRow :: User -> HTML Event
userRow (User user) =
H.tr ! key user.id ! A.className "user-item" $ do
...