Skip to main content

TTC Binary Builder Support

The Haskell community is currently discussing a proposal to support Unicode characters in instance Show String, and there is an RFC on the libraries mailing list. There was some discussion about use of a (Render) type class that targets end users, as opposed to Show, which should produce valid Haskell code. I wrote a message linking to TTC, which addresses this goal.

Viktor commented:

One promising feature of this is that the target type is not restricted to just String, but rather Render produces a Textual value, which can be a String, Text, ByteString, or a Text or Binary builder. I would prefer to also see a ByteString Builder as an option, but the high level design feels sound.

The Textual type class currently supports the following types:

The above comment swaps ByteString Builder and Binary Builder, but I realize that I do not have any information about Binary Builder in the documentation!

The Builder type in Data.Binary.Builder, exported from package binary, is a re-export of the ByteString Builder type. It is therefore already supported by TTC.

The documentation says “This now re-exports Builder.” (emphasis mine), so I wrote a quick test to confirm that it works with all of the GHC versions and Stackage snapshots that I test TTC against. For the test program, I used the minimal example from the TTC README. (Full Source)

{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

-- https://hackage.haskell.org/package/binary
import qualified Data.Binary.Builder as BB

-- https://hackage.haskell.org/package/bytestring
import qualified Data.ByteString.Lazy.Char8 as BSL8

-- https://hackage.haskell.org/package/text
import Data.Text (Text)

-- https://hackage.haskell.org/package/ttc
import qualified Data.TTC as TTC

------------------------------------------------------------------------------

newtype Username = Username Text

instance TTC.Render Username where
  render (Username t) = TTC.convert t

------------------------------------------------------------------------------

main :: IO ()
main = BSL8.putStrLn . BB.toLazyByteString . TTC.render $ Username "tcard"

This test program builds and runs with all of the tested GHC versions and Stackage snapshots.

I updated the TTC API documentation and README, and these documentation updates will be included in the next release. I also updated the Textual Type Class article.

Author

Travis Cardwell

Published

Tags