generic-lens-2.2.2.0: Generically derive traversals, lenses and prisms.

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.Product.Typed

Contents

- Lenses

Description

Derive lenses of a given type in a product.

Synopsis

- class HasType a s where
  - typed :: Lens s s a a
  - getTyped :: s -> a
  - setTyped :: a -> s -> s

# Lenses

## Running example:

    >>> :set -XTypeApplications
    >>> :set -XDataKinds
    >>> :set -XDeriveGeneric
    >>> import GHC.Generics
    >>> :m +Data.Generics.Internal.VL.Lens
    >>> :{
    data Human
      = Human
        { name    :: String
        , age     :: Int
        , address :: String
        , tall    :: Bool
        }
      | HumanNoTall
        { name    :: String
        , age     :: Int
        , address :: String
        }
      deriving (Generic, Show)
    human :: Human
    human = Human "Tunyasz" 50 "London" False
    :}

class HasType a s where Source #

Records that have a field with a unique type.

Minimal complete definition

typed | setTyped, getTyped

Methods

typed :: Lens s s a a Source #

A lens that focuses on a field with a unique type in its parent type. Compatible with the lens package's `Lens` type.

    >>> human ^. typed @Int
    50

### Type errors

    >>> human ^. typed @String
    ...
    ...
    ... The type Human contains multiple values of type [Char].
    ... The choice of value is thus ambiguous. The offending constructors are:
    ... Human
    ... HumanNoTall
    ...

    >>> human ^. typed @Bool
    ...
    ...
    ... Not all constructors of the type Human contain a field of type Bool.
    ... The offending constructors are:
    ... HumanNoTall
    ...

getTyped :: s -> a Source #

Get field at type.

setTyped :: a -> s -> s Source #

Set field at type.

#### Instances

Instances details

HasType a Void Source #

Uncluttering type signatures (see `Void`)

    >>> :t +d typed
    typed :: (HasType a s, Functor f) => (a -> f a) -> s -> f s

Note that this might not longer be needed given the 'HasType a a' instance.

Instance details Defined in Data.Generics.Product.Typed Methods typed :: Lens Void Void a a Source # getTyped :: Void -> a Source # setTyped :: a -> Void -> Void Source #
HasType a a Source # |
Instance details Defined in Data.Generics.Product.Typed Methods typed :: Lens a a a a Source # getTyped :: a -> a Source # setTyped :: a -> a -> a Source #
Context a s => HasType a s Source # |
Instance details Defined in Data.Generics.Product.Typed Methods typed :: Lens s s a a Source # getTyped :: s -> a Source # setTyped :: a -> s -> s Source #