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?