4

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.

Clark Gaebel
  • 17,280
  • 20
  • 66
  • 93

1 Answers1

3

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.

hammar
  • 138,522
  • 17
  • 304
  • 385