Copyright | (c) Winterland, 2016 |
---|---|
License | BSD |
Maintainer | drkoster@qq.com |
Stability | experimental |
Portability | PORTABLE |
Safe Haskell | Safe |
Language | Haskell2010 |
This module provide Has
class which provide simple extensible product.
The use case for this class is illustrated as following:
{-# LANGUAGE FlexibleContexts #-} -- in some library code ... logInAnyReaderHasLogger :: (Has Logger r, MonadReader r m) => LogString -> m () logInAnyReaderHasLogger s = asks getter >>= logWithLogger s queryInAnyReaderHasSQL :: (Has SqlBackEnd r, MonadReader r m) => Query -> m a queryInAnyReaderHasSQL q = asks getter >>= queryWithSQL q ... -- now you want to use these effects together ... logger <- initLogger ... sql <- initSqlBackEnd ... (`runReader` (logger, sql)) $ do ... logInAnyReaderHasLogger ... ... x <- queryInAnyReaderHasSQL ... ...
If you need multiple elements with same type, you can use tagged like:
(Has (Tagged "StdLogger" Logger) r, Has (Tagged "FileLogger" Logger) r, ...) => ... runYourMonad ... ( stdLogger :: Tagged "StdLogger" Logger , fileLogger :: Tagged "FileLogger" Logger, ...)
Or you can define newtypes(which is less verbose and require no dependency):
newtype StdLogger = StdLogger Logger newtype FileLogger = FileLogger Logger runYourMonad ... (StdLogger stdLogger, FileLogger fileLogger)
Polymorphic values, such as numeric and string literals(with OverloadedString Enabled) may lead to type inference failure, you simply need type annotations in these cases:
... (3 :: Int, "hello" :: String, ...)
Documentation
A type class for extensible product.
We provide instances for tuples up to 12 elements by default.
You can define your own instance of Has
, but most of the time tuples will do fine.
Has a a Source | |
Has b (a, b) Source | |
Has a (a, b) Source | |
Has c (a, b, c) Source | |
Has b (a, b, c) Source | |
Has a (a, b, c) Source | |
Has d (a, b, c, d) Source | |
Has c (a, b, c, d) Source | |
Has b (a, b, c, d) Source | |
Has a (a, b, c, d) Source | |
Has e (a, b, c, d, e) Source | |
Has d (a, b, c, d, e) Source | |
Has c (a, b, c, d, e) Source | |
Has b (a, b, c, d, e) Source | |
Has a (a, b, c, d, e) Source | |
Has f (a, b, c, d, e, f) Source | |
Has e (a, b, c, d, e, f) Source | |
Has d (a, b, c, d, e, f) Source | |
Has c (a, b, c, d, e, f) Source | |
Has b (a, b, c, d, e, f) Source | |
Has a (a, b, c, d, e, f) Source | |
Has g (a, b, c, d, e, f, g) Source | |
Has f (a, b, c, d, e, f, g) Source | |
Has e (a, b, c, d, e, f, g) Source | |
Has d (a, b, c, d, e, f, g) Source | |
Has c (a, b, c, d, e, f, g) Source | |
Has b (a, b, c, d, e, f, g) Source | |
Has a (a, b, c, d, e, f, g) Source | |
Has h (a, b, c, d, e, f, g, h) Source | |
Has g (a, b, c, d, e, f, g, h) Source | |
Has f (a, b, c, d, e, f, g, h) Source | |
Has e (a, b, c, d, e, f, g, h) Source | |
Has d (a, b, c, d, e, f, g, h) Source | |
Has c (a, b, c, d, e, f, g, h) Source | |
Has b (a, b, c, d, e, f, g, h) Source | |
Has a (a, b, c, d, e, f, g, h) Source | |
Has i (a, b, c, d, e, f, g, h, i) Source | |
Has h (a, b, c, d, e, f, g, h, i) Source | |
Has g (a, b, c, d, e, f, g, h, i) Source | |
Has f (a, b, c, d, e, f, g, h, i) Source | |
Has e (a, b, c, d, e, f, g, h, i) Source | |
Has d (a, b, c, d, e, f, g, h, i) Source | |
Has c (a, b, c, d, e, f, g, h, i) Source | |
Has b (a, b, c, d, e, f, g, h, i) Source | |
Has a (a, b, c, d, e, f, g, h, i) Source | |
Has j (a, b, c, d, e, f, g, h, i, j) Source | |
Has i (a, b, c, d, e, f, g, h, i, j) Source | |
Has h (a, b, c, d, e, f, g, h, i, j) Source | |
Has g (a, b, c, d, e, f, g, h, i, j) Source | |
Has f (a, b, c, d, e, f, g, h, i, j) Source | |
Has e (a, b, c, d, e, f, g, h, i, j) Source | |
Has d (a, b, c, d, e, f, g, h, i, j) Source | |
Has c (a, b, c, d, e, f, g, h, i, j) Source | |
Has b (a, b, c, d, e, f, g, h, i, j) Source | |
Has a (a, b, c, d, e, f, g, h, i, j) Source | |
Has k (a, b, c, d, e, f, g, h, i, j, k) Source | |
Has j (a, b, c, d, e, f, g, h, i, j, k) Source | |
Has i (a, b, c, d, e, f, g, h, i, j, k) Source | |
Has h (a, b, c, d, e, f, g, h, i, j, k) Source | |
Has g (a, b, c, d, e, f, g, h, i, j, k) Source | |
Has f (a, b, c, d, e, f, g, h, i, j, k) Source | |
Has e (a, b, c, d, e, f, g, h, i, j, k) Source | |
Has d (a, b, c, d, e, f, g, h, i, j, k) Source | |
Has c (a, b, c, d, e, f, g, h, i, j, k) Source | |
Has b (a, b, c, d, e, f, g, h, i, j, k) Source | |
Has a (a, b, c, d, e, f, g, h, i, j, k) Source | |
Has l (a, b, c, d, e, f, g, h, i, j, k, l) Source | |
Has k (a, b, c, d, e, f, g, h, i, j, k, l) Source | |
Has j (a, b, c, d, e, f, g, h, i, j, k, l) Source | |
Has i (a, b, c, d, e, f, g, h, i, j, k, l) Source | |
Has h (a, b, c, d, e, f, g, h, i, j, k, l) Source | |
Has g (a, b, c, d, e, f, g, h, i, j, k, l) Source | |
Has f (a, b, c, d, e, f, g, h, i, j, k, l) Source | |
Has e (a, b, c, d, e, f, g, h, i, j, k, l) Source | |
Has d (a, b, c, d, e, f, g, h, i, j, k, l) Source | |
Has c (a, b, c, d, e, f, g, h, i, j, k, l) Source | |
Has b (a, b, c, d, e, f, g, h, i, j, k, l) Source | |
Has a (a, b, c, d, e, f, g, h, i, j, k, l) Source |