5

In the yesod book there is a paragraph:

Template Haskell is essentially Haskell which generates a Haskell Abstract Syntax Tree (AST).

There’s actually more power in TH than that, as it can actually introspect code. We don’t use these facilities in Yesod, however.

What does it mean to introspect code and what can you do with this feature?

Community
  • 1
  • 1
Qwertie
  • 5,784
  • 12
  • 45
  • 89
  • Do you mean [this book](https://www.yesodweb.com/book)? Maybe add the link to your question? – jjj Dec 25 '17 at 14:39

1 Answers1

8

The Template Haskell code in Yesod is only used to generate code. 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.

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. 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.

Michael Snoyman
  • 31,100
  • 3
  • 48
  • 77