summaryrefslogtreecommitdiff
path: root/docs/users_guide/exts/overloaded_record_dot.rst
blob: 26f29b352c897277778b855ebf477e45786c64b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
.. _overloaded-record-dot:

Overloaded record dot
---------------------

.. extension:: OverloadedRecordDot
    :shortdesc: Record '.' syntax

    :since: 9.2.0

    Provides record '.' syntax e.g. ``x.foo``

When ``OverloadedRecordDot`` is enabled one can write ``a.b`` to mean the ``b`` field of the ``a`` record expression.

Example:

.. code-block:: haskell

  {-# LANGUAGE OverloadedRecordDot #-}
  {-# LANGUAGE DuplicateRecordFields #-}

  data Person = Person { name :: String }
  data Company = Company { name :: String, owner :: Person }

  main = do
    let c = Company { name = "Acme Corp."
                    , owner = Person { name = "Wile E. Coyote" } }
    print $ c.name ++ " is run by " ++ c.owner.name

You may also write ``(.b)`` to mean a function that "projects the ``b`` field from its argument". For example, ``(.b) a`` means the same thing as ``a.b``).

``OverloadedRecordDot`` is normally implemented by desugaring record ``.`` expressions to ``GHC.Records.getField`` expressions. By enabling ``OverloadedRecordDot`` and ``RebindableSyntax`` together it is possible to desugar ``.`` expressions into your own ``getField`` implementations.

When considering ``a.b``, the ``b`` field that is meant is determined by solving ``HasField`` constraints. See :ref:`solving-hasfield-constraints`.