Search code examples
haskellpretty-print

No space concatenating with an empty doc in wl-pprint


I'm using wl-pprint to output a data type with its fields rendered and separated with spaces. Some of the fields can be Nothing, so I render them as empty. Obviously, I don't want to add an extra space next to an empty string. It was easily achievable with HughesPJ library, as its <+> operator has empty as unit. In wl-pprint the space in <+> is not optional however.

It would be easy to implement myself, given a function to test if Doc is empty. My another question is didn't anyone ever need to check for an empty Doc? Is it considered to be a drawback of my algorithm design?


Solution

  • This answers the "test if Doc is empty" part.

    renderCompact gives you a SimpleDoc which you can pattern-match for SEmpty.

    It seems sufficiently lazy, e.g.

    case ( renderCompact $ vcat $ repeat $ text "foobar" ) of 
       SEmpty -> True ; _ -> False