HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulegeneric-optics-2.2.1.0Haskell2010

Data.Generics.Product.Fields

Derive record field getters and setters generically.

  • 3 classes
  • 2 values

Lenses

5 declarations

Running example:

Example8 expressions
:set -XTypeApplications:set -XDataKinds:set -XDeriveGeneric:set -XGADTs:set -XFlexibleContextsimport GHC.Genericsimport Optics.Core:{data Human a  = Human    { name    :: String    , age     :: Int    , address :: String    , other   :: a    }  | HumanNoAddress    { name    :: String    , age     :: Int    , other   :: a    }  deriving (Generic, Show)human :: Human Boolhuman = Human { name = "Tunyasz", age = 50, address = "London", other = False }:}
classclass HasField (field :: Symbol) s t a b | s field -> a, t field -> b, s field b -> t, t field a -> s where
#

Records that have a field with a given name.

Methods

  • field :: Lens s t a b

    A lens that focuses on a field with a given name.

    Example1 expression
    human ^. field @"age"50
    Type changing
    Example1 expression
    :t humanhuman :: Human Bool
    Example1 expression
    :t human & field @"other" .~ (42 :: Int)human & field @"other" .~ (42 :: Int) :: Human Int
    Example1 expression
    human & field @"other" .~ 42Human {name = "Tunyasz", age = 50, address = "London", other = 42}
    Type errors
    Example1 expression
    human & field @"weight" .~ 42...... The type Human Bool does not contain a field named 'weight'....
    Example1 expression
    human & field @"address" .~ ""...... Not all constructors of the type Human Bool... contain a field named 'address'.... The offending constructors are:... HumanNoAddress...
Instances2HasField
  • (Context field s t a b, HasField0 field s t a b) => HasField field s t a bDefined in generic-optics-2.2.1.0 · Data.Generics.Product.Fields
  • HasField f (Void1 a) (Void1 b) a bDefined in generic-optics-2.2.1.0 · Data.Generics.Product.Fields

    See Note [Uncluttering type signatures] >>> :t field field :: HasField field s t a b => Lens s t a b

classclass HasField' (field :: Symbol) s a | s field -> a where
#

Methods

Instances2HasField'
  • Context' field s a => HasField' field s aDefined in generic-optics-2.2.1.0 · Data.Generics.Product.Fields
  • HasField' f (Void1 a) aDefined in generic-optics-2.2.1.0 · Data.Generics.Product.Fields
classclass HasField_ (field :: Symbol) s t a b where
#

Records that have a field with a given name.

This is meant to be more general than HasField, but that is not quite the case due to the lack of functional dependencies.

The types s and t must be applications of the same type constructor. In contrast, HasField also requires the parameters of that type constructor to have representational roles.

One use case of HasField_ over HasField is for records defined with data instance.

Methods

Instances2HasField_
  • (Context_ field s t a b, HasField0 field s t a b) => HasField_ field s t a bDefined in generic-optics-2.2.1.0 · Data.Generics.Product.Fields
  • HasField_ f (Void1 a) (Void1 b) a bDefined in generic-optics-2.2.1.0 · Data.Generics.Product.Fields
valuegetField :: HasField' f s a => s -> a
#
Example1 expression
getField @"age" human50
valuesetField :: HasField' f s a => a -> s -> s
#
Example1 expression
setField @"age" 60 humanHuman {name = "Tunyasz", age = 60, address = "London", other = False}