2017-06-07 31 views
1

我有一個類型:錯誤無法比擬型

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 
    } 

回答

2

User是圍繞一個記錄一個NEWTYPE,所以它包裹起來記錄。爲了訪問記錄字段,你首先需要解開它。在您的情況下執行此操作的最簡單方法是在user參數上進行模式匹配:

userRow :: User -> HTML Event 
userRow (User user) = 
    H.tr ! key user.id ! A.className "user-item" $ do 
    ...