gpt4 book ai didi

haskell - 为什么 Haskell/GHC 不支持记录名重载

转载 作者:行者123 更新时间:2023-12-03 19:58:18 27 4
gpt4 key购买 nike

我是一个 Haskell 新手。我注意到 Haskell 不支持记录名称重载:

-- Records.hs

data Employee = Employee
{ firstName :: String
, lastName :: String
, ssn :: String
} deriving (Show, Eq)

data Manager = Manager
{ firstName :: String
, lastName :: String
, ssn :: String
, subordinates :: [Employee]
} deriving (Show, Eq)

当我编译这个时,我得到:
[1 of 1] Compiling Main             ( Records.hs, Records.o )

Records.hs:10:5:
Multiple declarations of `firstName'
Declared at: Records.hs:4:5
Records.hs:10:5

Records.hs:11:5:
Multiple declarations of `lastName'
Declared at: Records.hs:5:5
Records.hs:11:5

Records.hs:12:5:
Multiple declarations of `ssn'
Declared at: Records.hs:6:5
Records.hs:12:5

鉴于 Haskell 类型系统的“强度”,编译器似乎应该很容易确定要访问哪个字段
emp = Employee "Joe" "Smith" "111-22-3333"
man = Manager "Mary" "Jones" "333-22-1111" [emp]
firstName man
firstName emp

是否有一些我没有看到的问题。我知道 Haskell 报告不允许这样做,但为什么不呢?

最佳答案

历史原因。有have been many competing designs为 Haskell 提供更好的记录系统——事实上,no consensus可以到达。然而。

关于haskell - 为什么 Haskell/GHC 不支持记录名重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11100322/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com