Polysemy: Tagged effects
At some point you'll have a lot of effects, which is not an issue, however you might duplicated types (usually, I would advise to use newtype
, but you might not have the hand on the consumer code).
That's why you have the Tagged
effect:
newtype Tagged k e m a where
Tagged :: forall k e m a. e m a -> Tagged k e m a
We can use directly:
displayJoined = do
name <- tag @"name" @(Reader String) ask
location <- tag @"location" @(Reader String) ask
embed $ putStrLn $ "Your name is " <> name
embed $ putStrLn $ "Your location is " <> location
Or to wrap existing code:
displayName = do
name <- ask
embed $ putStrLn $ "Your name is " <> name
displayLocation = do
location <- ask
embed $ putStrLn $ "Your location is " <> location
displaySplit = do
tag @"name" @(Reader String) displayName
tag @"location" @(Reader String) displayLocation
Then we have untag
which unwraps the underlying effect:
runApp =
runM
. runReader "France"
. untag @"location"
. runReader "Gautier"
. untag @"name"
Finally, here's what it gives:
Your name is Gautier
Your location is France
See the full the code here.