简体   繁体   English

haskell中的代码内省是什么?

[英]What is code introspection in haskell?

In the yesod book there is a paragraph: yesod书中有一段:

Template Haskell is essentially Haskell which generates a Haskell Abstract Syntax Tree (AST). 模板Haskell本质上是Haskell,它生成Haskell抽象语法树(AST)。

There's actually more power in TH than that, as it can actually introspect code. TH中实际上有更多的功率,因为​​它实际上可以内省代码。 We don't use these facilities in Yesod, however. 但是,我们不在Yesod使用这些设施。

What does it mean to introspect code and what can you do with this feature? 内省代码是什么意思,你可以用这个功能做什么?

The Template Haskell code in Yesod is only used to generate code. Yesod中的Template Haskell代码仅用于生成代码。 In that sense, it's a strict replacement for boilerplate. 从这个意义上讲,它是样板的严格替代品。 Instead of using Template Haskell, we could write out manually a transformation from the route file syntax to code you should write by hand, and you could write the equivalent code yourself. 我们可以手动编写从路径文件语法到您应该手动编写的代码的转换,而不是使用Template Haskell,您可以自己编写等效代码。

With introspection, you actually look at existing information the compiler has and make decisions. 通过内省,您实际上可以查看编译器的现有信息并做出决策。 For example, you could search for all of the instances of the Show typeclass and create a String with that list. 例如,您可以搜索Show类型类的所有实例,并使用该列表创建一个String That kind of approach can be useful in some cases, such as auto-generating a set of tests. 在某些情况下,这种方法很有用,例如自动生成一组测试。 The comment in the book is just stating that Yesod never does this sort of thing. 书中的评论只是说Yesod从未做过这类事情。

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

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