简体   繁体   English

使用模板haskell获取范围内的所有函数/值

[英]Get all functions/values in scope with template haskell

With template haskell, is there a way to list all functions in scope? 使用模板haskell,是否可以列出作用域中的所有功能? 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. 我要尝试执行的操作是获取以test_开头的所有导入函数的列表,并自动运行测试。

Sadly, there is no such reflection capability in Template Haskell, but there are workarounds. 可悲的是,Template Haskell中没有这种反射功能,但是有解决方法。 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_ . 但是,在您自己编写类似代码之前,建议您尝试使用test-framework-th程序包,该程序包已经对以case_开头的HUnit测试以及以prop_开头的case_属性进行了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. 在后台,该程序包使用language-haskell-extract程序包,该程序包实际上在模块上运行其自己的解析过程以选择定义。 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. 但是,它确实带来了大量的依赖关系,并且额外的解析过程可能会减慢您的构建速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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