Copyright | (C) 2017 Csongor Kiss |
---|---|
License | BSD3 |
Maintainer | Csongor Kiss <kiss.csongor.kiss@gmail.com> |
Stability | experimental |
Portability | non-portable |
Safe Haskell | Safe |
Language | Haskell2010 |
Derive record field getters and setters generically.
- class HasType a s where
Lenses
Running example:
>>>
:set -XTypeApplications
>>>
:set -XDataKinds
>>>
:set -XDeriveGeneric
>>>
import GHC.Generics
>>>
:m +Data.Generics.Internal.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.
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 ...
Get field at type.
setTyped :: a -> s -> s Source #
Set field at type.