Copyright | (c) 2016, Peter Trško |
---|---|
License | BSD3 |
Maintainer | peter.trsko@gmail.com |
Stability | experimental |
Portability | CPP, DataKinds, DeriveDataTypeable, DeriveGeneric, FlexibleContexts (GHC <8), FlexibleInstances, LambdaCase, MagicHash (GHC <8), MultiParamTypeClasses, NoImplicitPrelude, RecordWildCards, TemplateHaskell, TupleSections, TypeFamilies, TypeSynonymInstances |
Safe Haskell | None |
Language | Haskell2010 |
Derive magic instances for OverloadedRecordFields.
- overloadedRecord :: DeriveOverloadedRecordsParams -> Name -> DecsQ
- overloadedRecords :: DeriveOverloadedRecordsParams -> [Name] -> DecsQ
- overloadedRecordFor :: Name -> (DeriveOverloadedRecordsParams -> DeriveOverloadedRecordsParams) -> DecsQ
- overloadedRecordsFor :: [Name] -> (DeriveOverloadedRecordsParams -> DeriveOverloadedRecordsParams) -> DecsQ
- data DeriveOverloadedRecordsParams = DeriveOverloadedRecordsParams {}
- fieldDerivation :: IsLabel "fieldDerivation" a => a
- type FieldDerivation = String -> String -> Word -> Maybe String -> Maybe OverloadedField
- data OverloadedField
- = GetterOnlyField String (Maybe ExpQ)
- | GetterAndSetterField String (Maybe (ExpQ, ExpQ))
- defaultFieldDerivation :: FieldDerivation
- defaultMakeFieldName :: String -> String -> Word -> Maybe String -> Maybe String
- field :: String -> TypeQ -> TypeQ -> TypeQ -> TypeQ -> ExpQ -> ExpQ -> DecsQ
- simpleField :: String -> TypeQ -> TypeQ -> ExpQ -> ExpQ -> DecsQ
- fieldGetter :: String -> TypeQ -> TypeQ -> ExpQ -> DecsQ
- fieldSetter :: String -> TypeQ -> TypeQ -> TypeQ -> TypeQ -> ExpQ -> DecsQ
- simpleFieldSetter :: String -> TypeQ -> TypeQ -> ExpQ -> DecsQ
- data DeriveFieldParams = DeriveFieldParams {
- typeName :: Name
- typeVariables :: [Name]
- constructorName :: Name
- numberOfArgs :: Word
- currentIndex :: Word
- accessorName :: Maybe Name
- strictness :: Strict
- fieldType :: Type
- deriveForConstructor :: DeriveOverloadedRecordsParams -> Name -> [TyVarBndr] -> Con -> DecsQ
- deriveForField :: DeriveOverloadedRecordsParams -> DeriveFieldParams -> DecsQ
- newNames :: Word -> String -> Q [Name]
- strTyLitT :: String -> TypeQ
- varEs :: [Name] -> [ExpQ]
- varPs :: [Name] -> [PatQ]
- wildPs :: Word -> [Pat]
Derive OverloadedRecordFields instances
:: DeriveOverloadedRecordsParams | Parameters for customization of deriving process. Use |
-> Name | Name of the type for which magic instances should be derived. |
-> DecsQ |
Derive magic OverloadedRecordFields instances for specified type.
:: DeriveOverloadedRecordsParams | Parameters for customization of deriving process. Use |
-> [Name] | Names of the types for which magic instances should be derived. |
-> DecsQ |
Derive magic OverloadedRecordFields instances for specified types.
:: Name | Name of the type for which magic instances should be derived. |
-> (DeriveOverloadedRecordsParams -> DeriveOverloadedRecordsParams) | Function that modifies parameters for customization of deriving process. |
-> DecsQ |
Derive magic OverloadedRecordFields instances for specified type.
Similar to overloadedRecords
, but instead of
DeriveOverloadedRecordsParams
value it takes function which can modify its
default value.
data Coordinates2D a { coordinateX :: a , coordinateY :: a }overloadedRecordsFor
''Coordinates2D $ #fieldDerivation .~ \_ _ _ -> \case Nothing -> Nothing Just field -> lookup field [ ("coordinateX",GetterOnlyField
"x" Nothing) , ("coordinateY",GetterOnlyField
"y" Nothing) ]
:: [Name] | Names of the types for which magic instances should be derived. |
-> (DeriveOverloadedRecordsParams -> DeriveOverloadedRecordsParams) | Function that modifies parameters for customization of deriving process. |
-> DecsQ |
Derive magic OverloadedRecordFields instances for specified types.
Customize Derivation Process
data DeriveOverloadedRecordsParams Source
Parameters for customization of deriving process. Use def
to get
default behaviour.
DeriveOverloadedRecordsParams | |
|
Generic DeriveOverloadedRecordsParams Source | |
Default DeriveOverloadedRecordsParams Source |
|
HasField "fieldDerivation" DeriveOverloadedRecordsParams FieldDerivation Source | |
ModifyField "fieldDerivation" DeriveOverloadedRecordsParams DeriveOverloadedRecordsParams FieldDerivation FieldDerivation Source | |
type Rep DeriveOverloadedRecordsParams Source | |
type FieldType "fieldDerivation" DeriveOverloadedRecordsParams = FieldDerivation Source | |
type UpdateType "fieldDerivation" DeriveOverloadedRecordsParams FieldDerivation = DeriveOverloadedRecordsParams Source |
fieldDerivation :: IsLabel "fieldDerivation" a => a Source
Overloaded label that can be used for accessing function of type
FieldDerivation
from DeriveOverloadedRecordsParams
.
This definition is available only if compiled with GHC <8.
type FieldDerivation Source
= String | Name of the type, of which this field is part of. |
-> String | Name of the constructor, of which this field is part of. |
-> Word | Field position as an argument of the constructor it is part of. Indexing starts from zero. |
-> Maybe String | Name of the field (record) accessor; |
-> Maybe OverloadedField | Describes how overloaded record field should be generated for this
specific constructor field. |
data OverloadedField Source
Describes what should be the name of overloaded record field, and can also provide custom implementation of getter and setter.
GetterOnlyField String (Maybe ExpQ) | Derive only getter instances. If second argument is |
GetterAndSetterField String (Maybe (ExpQ, ExpQ)) | Derive only getter instances. If second argument is |
Generic OverloadedField Source | |
HasField "fieldDerivation" DeriveOverloadedRecordsParams FieldDerivation Source | |
ModifyField "fieldDerivation" DeriveOverloadedRecordsParams DeriveOverloadedRecordsParams FieldDerivation FieldDerivation Source | |
type Rep OverloadedField Source | |
type UpdateType "fieldDerivation" DeriveOverloadedRecordsParams FieldDerivation = DeriveOverloadedRecordsParams Source |
defaultFieldDerivation :: FieldDerivation Source
Function used by default value of DeriveOverloadedRecordsParams
.
:: String | Name of the type, of which this field is part of. |
-> String | Name of the constructor, of which this field is part of. |
-> Word | Field position as an argument of the constructor it is part of. Indexing starts from zero. |
-> Maybe String | Name of the field (record) accessor; |
-> Maybe String | Overloaded record field name to be used for this specific constructor
field; |
Suppose we have a weird type definition as this:
data SomeType a b c = SomeConstructor { _fieldX :: a , someTypeFieldY :: b , someConstructorFieldZ :: c , anythingElse :: (a, b, c) }
Then for each of those fields, defaultMakeFieldName
will produce
expected OverloadedLabel name:
_fieldX --> fieldX
someTypeFieldY --> fieldY
someConstructorFieldZ --> fieldZ
anythingElse
is ignored
Low-level Deriving Mechanism
:: String | Overloaded label name. |
-> TypeQ | Record type. |
-> TypeQ | Field type. |
-> TypeQ | Record type after update. |
-> TypeQ | Setter will set field to a value of this type. |
-> ExpQ | Getter function. |
-> ExpQ | Setter function. |
-> DecsQ |
Derive instances for overloaded record field, both getter and setter.
:: String | Overloaded label name. |
-> TypeQ | Record type. |
-> TypeQ | Field type. |
-> ExpQ | Getter function. |
-> ExpQ | Setter function. |
-> DecsQ |
Derive instances for overloaded record field, both getter and setter. Same
as field
, but record type is the same before and after update and so is
the field type.
:: String | Overloaded label name. |
-> TypeQ | Record type. |
-> TypeQ | Field type |
-> ExpQ | Getter function. |
-> DecsQ |
Derive instances for overloaded record field getter.
:: String | Overloaded label name. |
-> TypeQ | Record type. |
-> TypeQ | Field type. |
-> TypeQ | Record type after update. |
-> TypeQ | Setter will set field to a value of this type. |
-> ExpQ | Setter function. |
-> DecsQ |
Derive instances for overloaded record field setter.
:: String | Overloaded label name. |
-> TypeQ | Record type. |
-> TypeQ | Field type. |
-> ExpQ | Setter function. |
-> DecsQ |
Derive instances for overloaded record field setter. Same as
fieldSetter
, but record type is the same before and after update and so is
the field type.
Internal Definitions
data DeriveFieldParams Source
Parameters for deriveForField
function.
DeriveFieldParams | |
|
:: DeriveOverloadedRecordsParams | Parameters for customization of deriving process. Use |
-> Name | |
-> [TyVarBndr] | |
-> Con | |
-> DecsQ |
Derive magic instances for all fields of a specific data constructor of a specific type.
:: DeriveOverloadedRecordsParams | Parameters for customization of deriving process. Use |
-> DeriveFieldParams | All the necessary information for derivation procedure. |
-> DecsQ |
Derive magic instances for a specific field of a specific type.