Haskell record accessors shared by different constructors of the same data type

百般思念 提交于 2019-12-10 10:28:44

问题


A basic question about Haskell records. If I define this datatype,

data Pet = Dog { name :: String } | Cat { name :: String } deriving (Show)                           

the following works:

main = do                                                                                            
  let d = Dog { name = "Spot" }                                                                      
      c = Cat { name = "Morris" }                                                                    
  putStrLn $ name d                                                                                  
  putStrLn $ name c                                                                                  

But if I do this,

data Pet = Dog { name :: String } | Cat { name :: Integer } deriving (Show)                          

I'll get this error: Multiple declarations of 'name'.

I think I understand intuitively why this should be the case, since the type of name in the first case is just Pet -> String regardless of the constructor that was used. But I don't recall seeing this rule about record accessor functions in any of the Haskell books I've read. Can someone give a slightly more in-depth explanation about the behavior I'm seeing above?


回答1:


From the Haskell '98 Report:

A data declaration may use the same field label in multiple constructors as long as the typing of the field is the same in all cases after type synonym expansion. A label cannot be shared by more than one type in scope. Field names share the top level namespace with ordinary variables and class methods and must not conflict with other top level names in scope.

I don't think there's anything more in-depth to it. As you said, the resulting field accessor has the type Pet -> String anyway so the powers that be decided it convenient to allow you to re-use the same field name in different constructors.



来源:https://stackoverflow.com/questions/20907974/haskell-record-accessors-shared-by-different-constructors-of-the-same-data-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!