Safe Haskell | None |
---|---|
Language | Haskell2010 |
Derive record field lenses generically.
Usage with labels in Optics.Label
Optics.Label provides LabelOptic
class which powers
the OverloadedLabels
instance for optics type.
It's possible to use HasField
and field
to derive instances
generically. If we have a simple record type which has Generic
instance:
data Ex = Ex { exA :: Int, exB :: Char } deriving (Generic)
We can derive LabelOptic
instances for all its fields
at once:
instance (HasField
name Ex a, a ~ b) => LabelOptic name A_Lens Ex Ex a b where labelOptic =field
@name
Note: GHC will ask you to enable a lot of extensions, do it.
Youn need to enable UndecidableInstances
in particular to make
FunctionalDependencies
check pass.
Documentation
field :: forall (name :: Symbol) (r :: Type) (a :: Type). HasField name r a => Lens' r a Source #
A lens that focuses on a field with a given name.
Compatible with the optics package's Lens
type.
Note: the lens is simple, i.e. doesn't allow type-changing updates. This keeps the implementation small and quick.
You also may want to specify
{-# OPTIONS_GHC -funfolding-keeness-factor=100 #-} (or some other arbitrarily large number)
for GHC to inline more aggressively.
class HasField (name :: Symbol) r a | name r -> a #
Type-class restricting field
usage.
field__
Instances
HasFieldInternal name r a => HasField name r a | |
Defined in Data.Generics.Lens.Lite | |
HasField name (Void1 a) a | |
Defined in Data.Generics.Lens.Lite |