| Copyright | (C) 2022-2023 Peter Lebbing |
|---|---|
| License | BSD2 (see the file LICENSE) |
| Maintainer | Peter Lebbing <peter@digitalbrains.com> |
| Safe Haskell | None |
| Language | Haskell2010 |
Prettyprinter.Interpolate
Contents
Description
Efficient interpolation for Prettyprinter
This module provides efficient interpolation of
string-interpolate
quasi quoters when used with
prettyprinters
Documents.
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 Strings, 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
Documentation
di :: QuasiQuoter Source #
__di :: QuasiQuoter Source #
diii :: QuasiQuoter Source #
__di'E :: QuasiQuoter Source #
__di'L :: QuasiQuoter Source #
diii'E :: QuasiQuoter Source #
diii'L :: QuasiQuoter Source #
Deprecated
d__i'E :: QuasiQuoter Source #