What is the Haskell library for interpolated strings

There are many different libraries in Hackage that use interpolated strings. Some of them are of poor quality, while others differ from the number of functions that they support.

Which ones are worth using?

Library examples (in a specific order): shakespeare , interpolatedstring-qq , Interpolation

+6
source share
1 answer

I looked at all the quasicoter interpolation libraries I could find in Hackage.

Interpolation libraries worth using:

  • interpolatedstring-perl6 : Supports interpolation of arbitrary Haskell code with reasonable syntax, but requires haskell-src-exts. If you just need the syntax of a common syntax string, I would use that.

  • shakespeare-text : based on the Shakespeare family of templates and has minimal dependencies; most other packages of interpolated strings depend on haskell-src-exts , which is a pretty heavy package and can take a lot of time and resources to compile. If you use any other Shakespeare patterns, I would advise going with this.

    However, it does not support interpolation of arbitrary Haskell code, although it does support more than a simple variable extension; It also acts as an application, operators, etc. I think it also uses Text , not String ; I'm not sure if it can be used with String looking from the source code, although there is support code to suggest that it could be.

  • Interpolation : supports arbitrary expressions (again with haskell-src-exts), and also has built-in looping tools. If you need more "template" functions than just simple interpolation, you should think about it, although I personally find the syntax pretty ugly.

Interpolation libraries are probably not worth using:

  • interpolatedstring-qq : It seems to be based on interpolatedstring-perl6; it has not been updated for more than a year and, apparently, has less functionality than string-perl6 interpolation. If you really are not attached to the syntax #{expr} , I would not consider this.

  • interpol : Implemented as a preprocessor, gives {foo} special meaning in strings; IMO is too heavy a decision, and with such easy syntax, it is likely to break existing code.

In general, I would suggest interolatedstring-perl6 if you don't mind the haskell-src-exts dependency and Shakespeare text if you do (or already use Shakespeare patterns).

Another option would be to use the string-qq package with a more general template; it supports String , Text and ByteString , which should cover any use. However, this clearly does not support embedding Haskell code, and you will need to specify the variables separately, so it will probably only be useful in certain situations.

+11
source

Source: https://habr.com/ru/post/906562/


All Articles