higgledy-0.2.1.0: Partial types as a type constructor.

Copyright(c) Tom Harding 2019
LicenseMIT
Maintainertom.harding@habito.com
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Data.Generic.HKD.Field

Description

 
Synopsis

Documentation

class HasField' (field :: Symbol) (f :: Type -> Type) (structure :: Type) (focus :: Type) | field f structure -> focus where Source #

When we work with records, all the fields are named, and we can refer to them using these names. This class provides a lens from our HKD structure to any f-wrapped field.

>>> :set -XDataKinds -XDeriveGeneric
>>> import Control.Lens ((&), (.~))
>>> import Data.Monoid (Last)
>>> import GHC.Generics
>>> data User = User { name :: String, age :: Int } deriving (Generic, Show)
>>> type Partial a = HKD a Last

We can create an empty partial User and set its name to "Tom" (which, in this case, is pure "Tom" :: Last String):

>>> mempty @(Partial User) & field @"name" .~ pure "Tom"
User {name = Last {getLast = Just "Tom"}, age = Last {getLast = Nothing}}

Thanks to some generic-lens magic, we also get some pretty magical type errors! If we create a (complete) partial user:

>>> import Data.Generic.HKD.Construction (deconstruct)
>>> total = deconstruct @Last (User "Tom" 25)

... and then try to access a field that isn't there, we get a friendly message to point us in the right direction:

>>> total & field @"oops" .~ pure ()
...
... error:
... • The type User does not contain a field named 'oops'.
...

Methods

field :: Lens' (HKD structure f) (f focus) Source #

Instances
(ErrorUnless field structure (CollectField field (HKD_ f structure)), GLens' (HasTotalFieldPSym field) (HKD_ f structure) (f focus)) => HasField' field f structure focus Source # 
Instance details

Defined in Data.Generic.HKD.Field

Methods

field :: Lens' (HKD structure f) (f focus) Source #