I only know Text.Printf, and didn’t have any problem using it. It help getting the jobs done (on some level) but wonder if there is some alternatives.
I always just use
(<>) together with
> name = "Eric" > age = 74 > "Hello, " <> name <> ". You are " <> show age <> "." "Hello, Eric. You are 74."
Or maybe using
> name = "Eric" > age = 74 > mconcat ["Hello, ", name, ". You are ", show age, "."] "Hello, Eric. You are 74."
I don’t see the need for any other formatting functions. Formatting functions might be slightly less verbose, but those need things like implicit type conversion and variadic functions which are just not worth the benefit in my opinion.
Not necessarily. Here is, for example, an interesting idea for formatting: https://hackage.haskell.org/package/formatting.
You can probably achieve something similar with Template Haskell and/or Quasi Quotter as well.
format in that package is a variadic function.
that does implicit type conversion. That is exactly what I find ugly. I think it is good that it is type-safe, but I still don’t like that it is variadic. And it is more verbose that the other printf-like functions which means that there is even less advantage over the solution that I use.
fmt library for fast and type-safe formatting:
But if you’re interested in Python-like formatting, there’s
Been meaning to try
string-interpolate for a while:
If you manage to give it a shot, it’d be nice to read about your experience.
@carlosdagos In our projects, we’re using neat-interpolation where package when we need to format a lot of text. See example in
summoner for the real-life use case.
neat-interpolation automatically removes common indentation, so it’s quite lovely.
There is a blog post and a discussion on /r/haskell regarding
string-interpolate package. Though it allows embedding calls to Haskell functions inside strings, this feature has its price. And the cost hear is a dependency on heavy
haskell-src-exts library which is officially not maintained anymore. I’m trying to optimise dependencies of my Haskell libraries and if I don’t need to format a lot, using just
<> is good enough actually! And
neat-interpolation is a relatively small library for this purpose.
In the case of that summoner example, I think it would be nicer to use a yaml library that can pretty print.
@jaror Not sure that YAML library with pretty-printing can support our style of formatting. As an author of the TOML configuration and parsing library, I understand how difficult it is to implement the library that supports various types of formatting users may want. Generating text is okay in our case: it doesn’t change often, it’s used only for couple files, so no need to bring another library.
At some point in the past, I was thinking about taking another route and implementing small interpolation DSL library. Because
neat-interpolation sometimes had undesirable behaviour.
Late reply but just wanted to say that you raised a really important point regarding
PyF 0.10 was just released and the biggest change is no more dependency to
haskell-src-exts if it may guide your choice