Created
April 18, 2018 21:32
-
-
Save Porges/b65eee73de15a5f19f548ca885351c95 to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE ConstraintKinds #-} | |
{-# LANGUAGE ImplicitParams #-} | |
{- declaration -} | |
data MyModuleType = | |
MkMyModule | |
{ mod_global1 :: Int | |
, mod_global2 :: String | |
} | |
-- ConstraintKinds lets us write an alias | |
-- for the implicit param: | |
type MyModule = (?myModule :: MyModuleType) | |
-- accessors for 'global' values: | |
global1 :: MyModule => Int | |
global1 = mod_global1 ?myModule | |
global2 :: MyModule => String | |
global2 = mod_global2 ?myModule | |
{- usage -} | |
main = | |
-- instantiate module | |
let ?myModule = MkMyModule 123 "456" in | |
print whatever | |
-- function that uses module implicitly | |
whatever :: MyModule => String | |
whatever = show global1 ++ global2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment