Copyright | (c) 2016, Peter Trško |
---|---|
License | BSD3 |
Maintainer | peter.trsko@gmail.com |
Stability | experimental |
Portability | CPP, DataKinds, DeriveDataTypeable, DeriveGeneric, 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
- 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
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.
Generic DeriveOverloadedRecordsParams Source | |
Default DeriveOverloadedRecordsParams Source |
|
SetField "fieldDerivation" DeriveOverloadedRecordsParams FieldDerivation Source | |
HasField "fieldDerivation" DeriveOverloadedRecordsParams FieldDerivation Source | |
type Rep DeriveOverloadedRecordsParams Source | |
type FieldType "fieldDerivation" DeriveOverloadedRecordsParams = FieldDerivation Source | |
type UpdateType "fieldDerivation" DeriveOverloadedRecordsParams FieldDerivation = DeriveOverloadedRecordsParams Source |
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 | |
SetField "fieldDerivation" DeriveOverloadedRecordsParams FieldDerivation Source | |
HasField "fieldDerivation" DeriveOverloadedRecordsParams 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.