The Overloaded Record Fields family of extensions for GHC allow multiple record datatypes to share the same field names, and make it possible for type information to disambiguate fields. There is no single OverloadedRecordFields
extension, but rather a family of related extensions:
-
DisambiguateRecordFields
: makes use of constructor names to disambiguate fields in record construction or pattern matching. -
DuplicateRecordFields
: permits a module to define the same field name in multiple datatypes. -
OverloadedLabels
: provides#foo
syntax for an identifier whose meaning is determined by typeclass instance resolution. Intended for use with theHasField
magic type class. -
NoFieldSelectors
: prevents fields being in scope as selector functions. (Not yet released as of January 2020.) -
RecordDotSyntax
: permitsexpression.field
syntax for record projection. (Not yet released as of January 2020.)
For user-facing documentation, see the GHC user's guide:
- DisambiguateRecordFields extension
- DuplicateRecordFields extension
- OverloadedLabels extension
- Record field selector polymorphism (HasField class)
For implementation status, see #18598, the tracking ticket for most recent work related to overloaded record fields, and the OverloadedRecordFields label.
For design discussion, see the GHC proposals:
-
Adding HasField class, changes to OverloadedLabels (Implemented in GHC 8.2 without
IsLabel x (r -> a)
instance) - Adding setField to HasField (Being implemented, see !3257)
- NoFieldSelectors (Being implemented for GHC 9.2, see !4743)
- RecordDotSyntax
- DuplicateRecordFields without ambiguous field access
Content previously on this page has been moved to the SORF page.
Code
- Prototype implementation of the magic typeclasses
-
Phab:D761, Phab:D1391, Phab:D1486, Phab:D1586, Phab:D1600:
DuplicateRecordFields
extension -
Phab:D1331, Phab:D1623:
OverloadedLabels
extension - Phab:D1687, Phab:D2708: magic classes
-
!3257: extension of
HasField
class to support updates -
!4532:
RecordDotSyntax
-
!4743:
NoFieldSelectors
and liberalisation ofDuplicateRecordFields
History
The extension was initially implemented in 2013 as a Google Summer of Code project, by Adam Gundry under the mentorship of Simon Peyton Jones.
- Simple Overloaded Record Fields (SORF), Simon PJ's original proposal
- Declared Overloaded Record Fields (DORF), a counterpoint proposal by Anthony Clayden
- Discussion of the problem and possible solutions
- Original design of the extension (2013)
-
Redesigned variant involving three extensions (2015)
- Part 1: DuplicateRecordFields (in GHC 8.0)
- Part 2: OverloadedLabels (in GHC 8.0)
- Part 3: Magic type classes (partly in GHC 8.2)
- Adam Gundry's blog post