Search code examples
haskellnamespacestypesrecords

Avoiding namespace pollution in Haskell


I'm using lots of different records in a program, with some of them using the same field names, e.g.

data Customer = Customer { ..., foo :: Int, ... }
data Product = Product { ..., foo :: Int, ... }

Now as the accessor function "foo" is defined twice, I get the "Multiple declarations" error. One way to avoid this would be using different modules that are imported fully qualified, or simply renaming the fields (which I don't want to do).

What is the officially suggested way of dealing with this in Haskell?


Solution

  • This is a very hairy problem. There are several proposals for fixing the record system. On a related note, see TDNR and related discussion on cafe.

    Using the currently available language features, I think the best option is defining the two types in two different modules, and doing a qualified import. On top of this, if you want, you can implement some type class machinery.

    In Customer.hs

    module Customer where
    data Customer = Customer { ..., foo :: Int, ... }
    

    In Product.hs

    module Product where
    data Product = Product { ..., foo :: Int, ... }
    

    While using them, in Third.hs

    module Third where
    
    import qualified Customer as C
    import qualified Product as P
    
    .. C.foo ..
    .. P.foo ..
    

    Yet, I imagine it won't be too late before you hit the problem about recursively dependent modules.