I’m super happy to announce PyF 0.10.0.1.
PyF
PyF is a text formatting library which was inspired by python f
strings.
It features:
- multi lines strings
- string interpolations
- formatting. It supports approximately all of the python
f
string formatting language. If you knowpython
, you knowPyF
. - optional common white space removal (ala “nix” strings)
- support for
OverloadedStrings
, so it generatesString
by default, but can generate anything you want. Actually, that’s a drop in replacement for the"string"
syntax. - type safety.
An example may be better than a thousand words:
*PyF> name = "Guillaume"
*PyF> age = 36
*PyF> putStrLn [fmt|Hello {name}. Your age in binary is {age:b} and pi equals {pi:.3f}.|]
Hello Guillaume. Your age in binary is 100100 and pi equals 3.142.
Details
Under the hood, it uses TemplateHaskell
because I needed the QuasiQuotes
for multi-lines support. One of the limitation of TemplateHaskell
is poor error reporting. Great care was taken about this and PyF reports errors at precise positions inside the quasi quotes too:
Here you can see that the interpolated expression in the middle of the quasi quotes is wrong, PyF reports this position correctly with a caret in the error message.
Unfortunately, there is no way for TemplateHaskell
code to emit located errors which can be interpreted by languages server (here haskell-language-server in vim), so it still reports the error at the beginning of the quasi quotes, but the correct location is displayed in the error message.
haskell-src-meta removal
The biggest change in 0.10 is the removal of haskell-src-exts
and haskell-src-meta
dependencies. PyF
is now based on the ghc
API.
The conversion from the ghc
API to template haskell AST is done internally, but I’m sure it can be interesting for any project using haskell-src-meta
. I opened a discussion on the GHC bug tracker about integrating this to ghc directly: HsExpr to Template Haskell (#19148) · Issues · Glasgow Haskell Compiler / GHC · GitLab . You are welcome to open a ticket on PyF
issue tracker if you want to help me releasing this sub library as an external library.
Changes in 0.10
Thank to the removal of haskell-src-xxx
(and the replacement of megaparsec
by parsec
), PyF have virtual 0 dependencies: it only depends on packages provided with ghc
. It was one of the main criticism I received about PyF (the second one was the usage of TemplateHaskell ;).
PyF also gained additional quasiquotes:
-
raw
is just raw multi-lines strings, so no escape chars, no formatting. -
str
is mult-lines string, escape chars, but no formatting. -
strTrim
andfmtTrim
are likestr
andfmt
, but they also remove leading whitespaces (like “nix” strings) so you can write multi-lines strings without breaking your indentation.
I’d like to thank @carlosdagos who initiate the rewrite without haskell-src-ext
.
And of course, PyF is compatible with GHC 9.2 which was just released, congratulation to all of you.
I hope you’ll find PyF interesting, I’m interested in any feedback in order to improve the library.
Happy formatting!