The type of view
in lens requires the argument Lens
to be of type Lens s s a a
.
It won't work with the general type Lens s t a b
. The reason for this is that you'd need to write many type signatures if the type of view
was generalized.
But you can just do what view does, without restricting the type:
import Control.Lens
import Control.Applicative -- This imports Const
idL :: Lens s t a b -> Lens s t a b
idL l = lens (\s -> getConst $ l Const s) (\s b -> set l b s)
I used Const
instead of the Accessor
type, so that code will work with lens HEAD too (which removed the Accessor
newtype)