我有一个类型:
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
}
答案 0 :(得分:3)
User
是记录周围的新类型,因此它将记录包装起来。要访问记录字段,首先需要打开它。在您的情况下执行此操作的最简单方法是在user
参数上进行模式匹配:
userRow :: User -> HTML Event
userRow (User user) =
H.tr ! key user.id ! A.className "user-item" $ do
...