Should lens generation be treated like instances

haskell, lenses

Solution

You don't need a newtype. There are actually many packages on hackage that define lenses for already existing types (for example, `xml-lens` or even `lens` itself).

The problem with defining instances is that there is no way to hide them. If you define lenses, you can just hide them when importing, like any other function:

import Module.Lens hiding (someGeneratedLens, ...)

This is not possible with instances (See https://stackoverflow.com/a/8731340/2494803 for reasons). Lenses are also not required to be globally unique, unlike instances.

Problem

I have a data type defined in another library. I would like to hook into that datatype with a lens generated by the Control.Lens library. Do I need to newtype my type in my code or is it considered safe to lens an already defined data type?

Original source

Related problems