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 ratherRender
produces aTextual
value, which can be a String, Text, ByteString, or a Text or Binarybuilder
. I would prefer to also see a ByteStringBuilder
as an option, but the high level design feels sound.
The Textual
type class currently supports the following
types:
String
- Strict
Text
- Lazy
Text
Text
Builder
- Strict
ByteString
- Lazy
ByteString
ByteString
Builder
ShortByteString
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
Username t) = TTC.convert t
render (
------------------------------------------------------------------------------
main :: IO ()
= BSL8.putStrLn . BB.toLazyByteString . TTC.render $ Username "tcard" main
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.