generic-lens-0.4.1.0: Generic data-structure operations exposed as lenses.

Copyright(C) 2017 Csongor Kiss
LicenseBSD3
MaintainerCsongor Kiss <kiss.csongor.kiss@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellSafe
LanguageHaskell2010

Data.Generics.Product

Contents

Description

Magic product operations using Generics

These classes need not be instantiated manually, as GHC can automatically prove valid instances via Generics. Only the Generic class needs to be derived (see examples).

Synopsis

Lenses

class HasAny sel a s | s sel k -> a where Source #

Records that have generic lenses.

Minimal complete definition

the

Methods

the :: Lens' s a Source #

A lens that focuses on a part of a product as identified by some selector. Currently supported selectors are field names, positions and unique types. Compatible with the lens package's Lens type.

>>> human ^. the @Int
50
>>> human ^. the @"name"
"Tunyasz"
>>> human ^. the @3
"London"

Instances

HasPosition i a s => HasAny Nat i a s Source # 

Methods

the :: Lens' s s Source #

HasField field a s => HasAny Symbol field a s Source # 

Methods

the :: Lens' s s Source #

HasType a s => HasAny * a a s Source # 

Methods

the :: Lens' s s Source #

class HasField field a s | s field -> a where Source #

Records that have a field with a given name.

Minimal complete definition

field | setField, getField

Methods

field :: Lens' s a Source #

A lens that focuses on a field with a given name. Compatible with the lens package's Lens type.

>>> human ^. field @"age"
50
>>> human & field @"name" .~ "Tamas"
Human {name = "Tamas", age = 50, address = "London"}

getField :: s -> a Source #

Get field

>>> getField @"name" human
"Tunyasz"

setField :: a -> s -> s Source #

Set field

>>> setField @"age" (setField @"name" "Tamas" human) 30
Human {name = "Tamas", age = 30, address = "London"}

Instances

(Generic s, ErrorUnless field s (HasTotalFieldP field (Rep s)), GHasField field (Rep s) a) => HasField field a s Source # 

Methods

field :: Lens' s a Source #

getField :: s -> a Source #

setField :: a -> s -> s Source #

class HasPosition i a s | s i -> a where Source #

Records that have a field at a given position.

Minimal complete definition

position | setPosition, getPosition

Methods

position :: Lens' s a Source #

A lens that focuses on a field at a given position. Compatible with the lens package's Lens type.

>>> human ^. position @1
"Tunyasz"
>>> human & position @2 .~ "Berlin"
Human {name = "Tunyasz", age = 50, address = "Berlin"}

getPosition :: s -> a Source #

Get positional field

>>> getPosition @1 human
"Tunyasz"

setPosition :: a -> s -> s Source #

Set positional field

>>> setPosition @2 (setField @1 "Tamas" human) 30
Human "Tamas" 30 "London"

Instances

(Generic s, ErrorUnless i s ((&&) ((<?) 0 i) ((<=?) i (Size (Rep s)))), GHasPosition 1 i (Rep s) a) => HasPosition i a s Source # 

Methods

position :: Lens' s a Source #

getPosition :: s -> a Source #

setPosition :: a -> s -> s Source #

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

getTyped :: s -> a Source #

Get field at type.

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

Set field at type.

Instances

(Generic s, ErrorUnlessOne a s (CountTotalType a (Rep s)), GHasType (Rep s) a) => HasType a s Source # 

Methods

typed :: Lens' s a Source #

getTyped :: s -> a Source #

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

Subtype relationships

class Subtype sup sub where Source #

Structural subtype relationship

sub is a (structural) subtype of sup, if its fields are a subset of those of sup.

Minimal complete definition

super | smash, upcast

Methods

super :: Lens' sub sup Source #

Structural subtype lens. Given a subtype relationship sub :< sup, we can focus on the sub structure of sup.

>>> human ^. super @Animal
Animal {name = "Tunyasz", age = 50}
>>> set (super @Animal) (Animal "dog" 10) human
Human {name = "dog", age = 10, address = "London"}

upcast :: sub -> sup Source #

Cast the more specific subtype to the more general supertype

>>> upcast human :: Animal
Animal {name = "Tunyasz", age = 50}

smash :: sup -> sub -> sub Source #

Plug a smaller structure into a larger one

>>> smash (Animal "dog" 10) human
Human {name = "dog", age = 10, address = "London"}

Instances

(GSmash * (Rep a) (Rep b), GUpcast (Rep a) (Rep b), Generic a, Generic b) => Subtype b a Source # 

Methods

super :: Lens' a b Source #

upcast :: a -> b Source #

smash :: b -> a -> a Source #