Created
December 2, 2022 22:15
-
-
Save ramirez7/60aa92aa8e0e1f368e0e6bef69bc2591 to your computer and use it in GitHub Desktop.
Using ImplicitParams to guide type inference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- asks is frequently too ambiguous | |
| impAsks | |
| :: forall a b es | |
| . (?impReader :: Proxy a) | |
| => Reader a :> es | |
| => (a -> b) | |
| -> Eff es a | |
| impAsks = asks | |
| withImpReader | |
| :: forall a k | |
| . (?impReader :: Proxy a => k) | |
| -> k | |
| withImpReader k = let ?impReader = Proxy @a in k | |
| -- This example contains usage of 'asks' that would | |
| -- normally be ambiguous but are not due to the implicit param | |
| example :: Eff '[Reader Int, Reader String] Int | |
| example = | |
| withImpReader @Int $ do | |
| x <- impAsks (+1) | |
| withImpReader @String $ do | |
| s <- impAsks (++ "hello world") | |
| pure (x + length s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment