Personal tools

Name clashes in record fields

From HaskellWiki

Revision as of 08:51, 14 February 2017 by Wizzup (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


1 Question

error| Multiple declarations of ‘xxx’

I like to define:

data Human = Human {name :: String}
data Dog = Dog {name :: String}

Why is this forbidden?

I like to define:

data Human = Human {name :: String}
name :: Cat -> String
name = ...

Why is this forbidden, too?

2 Answer

The record field accessors
are just functions

that retrieve the field's value from a particular record. They are in the global scope together with top-level functions and thus cannot have the same name. For resolving this you may:

  • rename the accessor or the top-level function
  • put the data declaration or the top-level function in another module and import qualified
  • write a typeclass with a
    function and fit the non-accessor function
    somehow into that.

2.1 Using language extension

DuplicateRecordFields extension (GHC 8.0.1+) allow definition of record types with identically-named fields.

3 See also