Haskell中自定义数据类型的模糊发生编译错误

时间:2016-12-20 23:33:44

标签: haskell lambda

我有自定义数据类型Person and Employee(DataTypesAPI.hs)

{-# LANGUAGE DuplicateRecordFields #-}
data Person = Person {fName :: String
                     , lName :: String
                     } deriving (Show, Generic, FromJSON, ToJSON, ToBSON, FromBSON)

data Employee = Employee {fName  :: String
                         , lName :: String
                         , id    :: String
                         } deriving (Show, Generic, FromJSON, ToJSON, ToBSON, FromBSON)

我有一个Employee实例(作为e),并希望从中创建Person实例,以便从Employee填充fName和lName。代码片段是(src / MyLib.hs)

import DataTypesAPI.hs (Employee(..), Person(..))
DataLink.map (\e -> Person (fName e) (lName e))

但是,由于两种类型都具有相同的属性,因此名称ghc会出现编译错误

Ambiguous occurrence ‘last_commit_hash’
It could refer to either the field ‘fName’,
                                 imported from ‘FirstHaskellAPI’ at src/MyLib.hs:68:101-115
                              or the field ‘fName’,
                                 imported from ‘FirstHaskellAPI’ at src/MyLib.hs:68:53-98

在我的代码的后半部分,我需要两种类型。如何解决这样的编译错误?

1 个答案:

答案 0 :(得分:0)

尝试使用例如消除歧义。

DataLink.map (\e -> Person (fName e) (lName (e :: Employee)))