| Copyright | (c) Andrey Mulik 2020-2021 |
|---|---|
| License | BSD-style |
| Maintainer | work.a.mulik@gmail.com |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Data.MField
Contents
Description
Data.MField provides mutable field type for record-style operations.
Synopsis
- module Data.Property
- module Data.Field
- data MField m record a where
- class IsRef ref where
- data MFieldRef m a
- data MFieldRep m a
- getter :: MField m record a -> Field m record (m a)
- setter :: MField m record a -> Field m record (a -> m ())
- modifier :: MField m record a -> Field m record ((a -> a) -> m a)
- modifierM :: MField m record a -> Field m record ((a -> m a) -> m a)
Exports
module Data.Property
module Data.Field
Mutable field
data MField m record a where Source #
MField represents field with mutable value and accessors. Unlike fields of
type Field, MField fields are undesirable (and sometimes impossible) to be
stored in the global environment. The dynamic nature of MField means
changing accessors for specific records, so accessors do not contain a record
argument.
MField isn't designed for accessing external abstract objects and interfaces,
it doesn't give the same freedom as Field, but at the same time it allows
you to create flexible connections between fields and values without having to
reconcile them.
MField contains a function that takes a record and returns an MFieldRef.
Example:
newtype HasField a = HasField {someField :: MFieldRep IO a}
field :: MField IO (HasField a) a
field = MField (ref.someField)
main :: IO ()
main = do
putStrLn "Put some number"
record <- fmap HasField $ link =<< var =<< readLn :: IO (HasField Integer)
print =<< get field record
set record [getter field :~ fmap (* 2)]
print =<< get field record
Since: 0.2
Instances
| FieldSwitch MField Source # | |
Defined in Data.MField | |
| FieldModify MField Source # | |
Defined in Data.MField | |
| FieldSet MField Source # | |
| FieldGet MField Source # | |
class IsRef ref where Source #
Methods
link :: MonadVar m => Var m a -> m (ref m a) Source #
Create new reference to the given variable.
ref :: MonadVar m => ref m a -> MFieldRef m a Source #
Get an associated MFieldRef reference to the given reference.
MFieldRep is a helper type that stores a variable and its accessors.