문제

There are many different libraries on Hackage dealing with interpolated strings. Some have poor quality while other vary with number of features they support.

Which ones are worth using?

Examples of libraries (in no particular order): shakespeare, interpolatedstring-qq, Interpolation

도움이 되었습니까?

해결책

I took a look at all the interpolation quasiquoter libraries I could find on Hackage.

Interpolation libraries worth using:

  • interpolatedstring-perl6: Supports interpolating arbitrary Haskell code with reasonable syntax, but requires haskell-src-exts. If you just want a general string interpolation syntax, I'd use this.

  • shakespeare-text: Based on the Shakespeare family of templates, and has minimal dependencies; most other interpolated string packages depend on haskell-src-exts, which is quite a heavy package and can take a lot of time and resources to compile. If you use any other Shakespeare templates, I'd suggest going with this one.

    However, it doesn't support interpolating arbitrary Haskell code, although it supports more than simple variable expansion; it also does function application, operators, etc. I think it also uses Text rather than String; I'm not sure whether it can be used with Strings looking from the source code, though there is support code to suggest it can be.

  • Interpolation: Supports arbitrary expressions (again with haskell-src-exts), and also has built-in looping facilities. If you want more "template"-like features than just plain interpolation, it's worth considering, although I personally find the syntax quite ugly.

Interpolation libraries probably not worth using:

  • interpolatedstring-qq: Seems to be based on interpolatedstring-perl6; it hasn't been updated for over a year, and seems to have less functionality than interpolatedstring-perl6. Unless you're really attached to the #{expr} syntax, I wouldn't consider this one.

  • interpol: Implemented as a preprocessor, gives {foo} special meaning in strings; IMO too heavyweight a solution, and with such lightweight syntax, likely to break existing code.

In summary, I'd suggest interpolatedstring-perl6 if you don't mind the haskell-src-exts dependency, and shakespeare-text if you do (or are already using Shakespeare templates).

Another option might be to use the string-qq package with a more general template engine; it supports String, Text and ByteString, which should cover every use. However, this obviously doesn't support embedding Haskell code, and you'll need to specify the variables separately, so it's probably only useful in certain situations.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top