OverloadedRecordDot and Lenses

Hello, does OverloadedRecordDot (GHC extension since 9.2) replace lenses concepts or are they meant to be use together ?

At work we use them together (with the optics library):

  • Dot accessors for “simple” access (record.subrecord.field)
  • Optics for more complex access (preview especially). One usecase is the implementation of a PATCH endpoint where the values of the resource in database are substituted by the fields present in the payload, and left alone if the payload value is set to Nothing.
  • Optics for record updates
1 Like

I have also recently started using OverloadedRecordDot at work, where we also use lens. My experience has been quite similar to the above:

  • Dot accessors are extremely comfortable and work well with (<&>) — but sensitive to autoformatting breaking code if pasted into a module without {-# LANGUAGE OverloadedRecordDot #-}
  • Record updates are easier with lens functions, especially (?~) and (%~). We have helper functions for preadjusting settings with an argument [settings -> settings]; we fill this list with things like fields <>~ [x, y, z] and name ?~ value.

We haven’t yet run into naming issues between field accessors and lenses, though it would be nice to be able to use the same name in both places: datatype.field and datatype ^. field.

1 Like