[Haskell-cafe] Shadowing record field names

Moritz Schulte mtesseract at silverratio.net
Sat Dec 23 14:06:45 UTC 2017


Hi Josh,

the current record-situation is not entirely satisfying, I agree.

Some time ago I wrote down the approach I currently use in this gist:
https://gist.github.com/mtesseract/1b69087b0aeeb6ddd7023ff05f7b7e68.

In a nutshell:

* I use -XDuplicateRecordFields such that I can in fact do this:

> data Whole1 = Whole1 { part :: Part, ... }
> data Whole2 = Whole2 { part :: Part, ... }

* I prefix all record fields with an underscore.

* I use lens' makeFieldsNoPrefix to generate lenses for the fields
  without the need to do any prefixing.

* I import lenses qualified as 'L'.

This means:

* Different records can have the field _author (which I basically only
  use directly during record construction).

* I usually access the author field of a record using record^.L.author.

* I can use a local binding named 'author' just fine, as it clashes
  neither with the record field (_author), nor with the lens
  (L.author).

Not perfect, but so far it's working fine for me.

Best,
Moritz


More information about the Haskell-Cafe mailing list