Copyright | (C) 2020 Csongor Kiss |
---|---|
License | BSD3 |
Maintainer | Csongor Kiss <kiss.csongor.kiss@gmail.com> |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Data.Generics.Labels
Description
Provides an (orphan) IsLabel instance for field lenses and constructor prisms, as well as positional lenses on GHC >=9.6. Use at your own risk.
Synopsis
- class Field (name :: k) s t a b | s name -> a, t name -> b, s name b -> t, t name a -> s where
- type Field' (name :: k) s a = Field name s s a a
- class Constructor (name :: k) s t a b | name s -> a, name t -> b where
- constructorPrism :: Prism s t a b
- type Constructor' (name :: k) s a = Constructor name s s a a
Orphan IsLabel Instance
An instance for creating lenses and prisms with #identifiers
from the
OverloadedLabels
extension. Note that since overloaded labels did not
support symbols starting with capital letters, all prisms (which come from
constructor names, which are capitalized) must be prefixed with an underscore
(e.g. #_ConstructorName
) when you use a GHC older than 9.6.
Morally:
instance (HasField name s t a b) => IsLabel name (Lens s t a b) where ...
and
instance (AsConstructor name s t a b) => IsLabel name (Prism s t a b) where ...
Starting with GHC 9.6, you can also write e.g. #2
and #15
instead of
position @1
and position @15
, so we morally have
instance (HasPosition i s t a b) => IsLabel (Show i) (Lens s t a b) where ...
Remember:
type Lens = forall f. Functor f => (a -> f b) -> s -> f t type Prism s t a b = forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t)
The orphan instance is unavoidable if we want to work with
lenses-as-functions (as opposed to a ReifiedLens
-like newtype).
class Field (name :: k) s t a b | s name -> a, t name -> b, s name b -> t, t name a -> s where Source #
class Constructor (name :: k) s t a b | name s -> a, name t -> b where Source #
Constructor
is morally the same as AsConstructor
, but it is constructed from an
incoherent combination of AsConstructor
and AsConstructor'
. In this way, it can be
seamlessly used in the IsLabel
instance even when dealing with data types
that don't have Constructor
instances (like data instances).
Methods
constructorPrism :: Prism s t a b Source #
Instances
AsConstructor' name s a => Constructor (name :: Symbol) s s a a Source # | |
Defined in Data.Generics.Labels Methods constructorPrism :: Prism s s a a Source # | |
AsConstructor name s t a b => Constructor (name :: Symbol) s t a b Source # | |
Defined in Data.Generics.Labels Methods constructorPrism :: Prism s t a b Source # |
type Constructor' (name :: k) s a = Constructor name s s a a Source #