The main question is about Haskell records. If I define this data type,
data Pet = Dog { name :: String } | Cat { name :: String } deriving (Show)
The following work is carried out:
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 will get this error: Multiple declarations of 'name' .
I think I intuitively understand why this should be so, since the type name in the first case is just Pet -> String regardless of the constructor used. But I donβt remember how I saw this rule about the write access functions in any of the Haskell books that I read. Can someone give a more detailed explanation of the behavior that I see above?
source share