What's so bad about Template Haskell?

后端 未结 6 1752
星月不相逢
星月不相逢 2020-12-12 09:31

It seems that Template Haskell is often viewed by the Haskell community as an unfortunate convenience. It\'s hard to put into words exactly what I have observed in this rega

6条回答
  •  失恋的感觉
    2020-12-12 10:02

    Why is TH bad? For me, it comes down to this:

    If you need to produce so much repetitive code that you find yourself trying to use TH to auto-generate it, you're doing it wrong!

    Think about it. Half the appeal of Haskell is that its high-level design allows you to avoid huge amounts of useless boilerplate code that you have to write in other languages. If you need compile-time code generation, you're basically saying that either your language or your application design has failed you. And we programmers don't like to fail.

    Sometimes, of course, it's necessary. But sometimes you can avoid needing TH by just being a bit more clever with your designs.

    (The other thing is that TH is quite low-level. There's no grand high-level design; a lot of GHC's internal implementation details are exposed. And that makes the API prone to change...)

提交回复
热议问题