Question

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?

Était-ce utile?

La 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top