modulespection: Template Haskell introspecting a modules declarations
Collect all of the declarations in a module using Template Haskell (via the GHC API).
One can either get all the names, or just the declarations (only type declarations are supported right now).
Here is a quick example
import Language.Haskell.TH.Module.Magic (names) data Test = Test Int newtype OtherTest = OtherTest Test someFunction :: String -> String someFunction = id -- 'names' is Template Haskell function that will collect all of the -- toplevel declaration names of the current file. names >>= runIO . print >> return []
Which will spew the following when compiling.
[Test,OtherTest,someFunction]
There is also declarations
which can be used, for example, to make sure that all
types have ToJSON
/FromJSON
instances.
import Data.Aeson.TH (deriveJSON, defaultOptions) import MonadUtils (concatMapM) import Language.Haskell.TH.Module.Magic (names) data Test = Test Int newtype OtherTest = OtherTest Test concatMapM (deriveJSON defaultOptions) =<< declarations
Which will makes the JSON instances for Test and OtherTest and any other types added to the file.
You can also do the same thing for an existing module.
import Data.Aeson.TH (deriveJSON, defaultOptions) import MonadUtils (concatMapM) import Language.Haskell.TH.Module.Magic (names) import Data.Monoid concatMapM (deriveJSON defaultOptions) =<< moduleDeclarations "Data.Monoid"
Which will build instances for all the types in Data.Monoid.
Downloads
- modulespection-0.1.0.0.tar.gz [browse] (Cabal source package)
- Package description (as included in the package)
Maintainer's Corner
For package maintainers and hackage trustees
Candidates
- No Candidates
Versions [RSS] | 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.0.4, 0.1.0.5, 0.1.0.6, 0.1.0.7, 0.1.1.0, 0.1.2.0, 0.1.2.1, 0.1.2.2 |
---|---|
Dependencies | base (>=4.6 && <4.7), ghc, ghc-paths (>=0.1 && <0.2), template-haskell (>=2.7 && <2.9) [details] |
License | BSD-3-Clause |
Author | Jonathan Fischoff |
Maintainer | jonathangfischoff@gmail.com |
Category | Language |
Home page | https://github.com/jfischoff/modulespection |
Uploaded | by JonathanFischoff at 2013-12-26T04:11:44Z |
Distributions | |
Reverse Dependencies | 2 direct, 0 indirect [details] |
Downloads | 7960 total (25 in the last 30 days) |
Rating | (no votes yet) [estimated by Bayesian average] |
Your Rating | |
Status | Docs available [build log] Successful builds reported [all 1 reports] |