Data.Global
Description
Declare safe top-level mutable variables which scope like ordinary values.
- declareIORef :: DeclareInit
- declareMVar :: DeclareInit
- declareEmptyMVar :: Declare
- declareSampleVar :: DeclareInit
- declareChan :: Declare
- declareQSem :: DeclareSem
- declareQSemN :: DeclareSem
- declareTVar :: DeclareInit
- declareTMVar :: DeclareInit
- declareEmptyTMVar :: Declare
- declareTChan :: Declare
- type Declare = String -> Q Type -> Q [Dec]
- type DeclareInit = String -> Q Type -> Q Exp -> Q [Dec]
- type DeclareSem = String -> Q Exp -> Q [Dec]
Using this module
Declare a top-level variable like so:
import Data.Global
import Control.Concurrent
declareChan "ch" [t| Maybe Char |]
main = do
writeChan ch (Just 'x')
readChan ch >>= print
This will create a module-level binding
ch :: Chan (Maybe Char)
The declareChan syntax is a Template Haskell declaration splice. The type of
channel contents is given inside a Template Haskell type quotation. The
TemplateHaskell extension must be enabled.
The scope of this variable can be controlled through the usual module
import/export mechanism. If another module defines a also named Chanch,
there is no implicit relationship between the two.
Some declarations take an initalizer as an expression quotation. The variable will initially hold an unevaluated thunk for this expression.
declareIORef "ref"
[t| Int |]
[e| 3 |]
main = do
readIORef ref >>= print
writeIORef ref 5
readIORef ref >>= print
For safety, it's important not to create polymorphic references. As a
conservative restriction, this library statically forbids syntactically
polymorphic types for reference contents. If you need to store polymorphic
values in a reference, you can create a wrapper type with
-XPolymorphicComponents.
IORef
declareIORef :: DeclareInitSource
Declare an with an initial value.
IORef
declareIORef "foo" [t| Char |] [e| 'x' |]
Control.Concurrent
declareMVar :: DeclareInitSource
Declare an with an initial value.
MVar
declareMVar "foo" [t| Char |] [e| 'x' |]
declareEmptyMVar :: DeclareSource
Declare an empty .
MVar
declareEmptyMVar "foo" [t| Char |]
declareSampleVar :: DeclareInitSource
Declare a with an initial value.
SampleVar
declareSampleVar "foo" [t| Char |] [e| 'x' |]
Declare an empty .
Chan
declareChan "foo" [t| Char |]
declareQSem :: DeclareSemSource
Declare a with the specified quantity.
QSem
declareQSem "foo" [e| 3 |]
declareQSemN :: DeclareSemSource
Declare a with the specified quantity.
QSemN
declareQSemN "foo" [e| 3 |]
STM
declareTVar :: DeclareInitSource
Declare a with an initial value.
TVar
declareTVar "foo" [t| Char |] [e| 'x' |]
declareTMVar :: DeclareInitSource
Declare a with an initial value.
TMVar
declareTMVar "foo" [t| Char |] [e| 'x' |]
declareEmptyTMVar :: DeclareSource
Declare an empty .
TMVar
declareEmptyTMVar "foo" [t| Char |]
Declare an empty .
TChan
declareTChan "foo" [t| Char |]