Copyright | (C) 2022 Peter Lebbing |
---|---|
License | BSD2 (see the file LICENSE) |
Maintainer | Peter Lebbing <peter@digitalbrains.com> |
Safe Haskell | None |
Language | Haskell2010 |
Efficient interpolation for Prettyprinter
This module provides efficient interpolation of
string-interpolate
quasi quoters when used with
prettyprinter
s
Doc
uments.
The normal quasi quoters from string-interpolate
do work when used as a
Doc
. Newlines are even converted to Prettyprinter.
line
.
However, this method is inefficient. The following code functions correctly:
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} module Main where import Data.String.Interpolate import Data.Text (Text) import Prettyprinter f ::Text
f = "world" g :: Doc () g = [i
|Hello #{f}!|] main :: IO () main = print g
However, what happens under the hood is that f
is converted to String
, the
interpolated string is built manipulating String
s, and then the output is
converted to Text
in
prettyprinter
. The
following code is much better:
g = pretty
([i|Hello #{f}!|] :: Text)
Now, the interpolated string is constructed as Text
, and this is passed
cleanly into Doc
which also uses Text
as its underlying type for
representation of text. At no point is f
converted to String
, and the
string construction benefits from the performance of Text
. And again,
newlines are converted to line
.
This module defines wrapper quasi quoters that automatically perform the
pretty
invocation, and can simply be used as:
g = [di
|Hello #{f}!|]
Synopsis
- di :: QuasiQuoter
- __di :: QuasiQuoter
- diii :: QuasiQuoter
- d__i'E :: QuasiQuoter
- d__i'L :: QuasiQuoter
- diii'E :: QuasiQuoter
- diii'L :: QuasiQuoter
Documentation
di :: QuasiQuoter Source #
__di :: QuasiQuoter Source #
diii :: QuasiQuoter Source #
d__i'E :: QuasiQuoter Source #
d__i'L :: QuasiQuoter Source #
diii'E :: QuasiQuoter Source #