简体   繁体   中英

Get all functions/values in scope with template haskell

With template haskell, is there a way to list all functions in scope? Something like

allVarsInScope :: Q [Name]

What I'm trying to do with this is get a list of all imported functions beginning with test_, and run the tests automatically.

Sadly, there is no such reflection capability in Template Haskell, but there are workarounds. However, before writing something like this yourself, I recommend trying the test-framework-th package which already does this for HUnit tests beginning with case_ and also for QuickCheck properties beginning with prop_ .

Under the hood, this package uses the language-haskell-extract package which essentially runs its own parsing pass over the module to pick out the definitions. It's a somewhat hacky solution, but it works well enough in practice. However, it does bring in a fair number of dependencies and the additional parsing pass can slow down your builds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM