A toString
converter for String-like types and any Show a => a
type.
branch | Travis status |
---|---|
master |
|
develop |
Add this library to extra-deps
in your stack.yaml
like the following if you use Stack.
...
extra-deps:
- git: https://github.com/nwtgck/to-string-haskell.git
commit: 0c8f2951606e185feacddb28983b30c527c3eb17
...
toString 89 == "89"
toString "hello" == "hello"
{-# LANGUAGE OverloadedStrings #-}
toString ("I'm a ByteString" :: BS.ByteString) == ("I'm a ByteString" :: String)
{-# LANGUAGE OverloadedStrings #-}
toString ("I'm a Text" :: T.Text) == ("I'm a Text" :: String)
String
Data.ByteString.Char8.ByteString
Data.ByteString.Lazy.Char8.ByteString
Data.ByteString.Short.ShortByteString
Data.ByteString.UTF8.ByteString
Data.ByteString.Lazy.UTF8.ByteString
Data.Text.Text
Data.Text.Lazy.Text
Any Show a => a
type is also an instance of type class ToString
.
So Int
, Double
, Maybe a
... which are Show
instances are also instances of ToString
{-# LANGUAGE OverloadedStrings #-}
import Data.String.ToString
import qualified Data.ByteString as BS
import qualified Data.Text as T
-- (This is an orignal type deriving `Show`)
data MyMaybe a =
MyJust a
| MyNothing
deriving Show
main :: IO ()
main = do
let i :: Int
i = 10
putStrLn (toString i)
-- => 10
let ch :: Char
ch = 'd'
putStrLn (toString ch)
-- => 'd'
let myMay1 :: MyMaybe Double
myMay1 = MyJust 1.89
putStrLn (toString myMay1)
-- => MyJust 1.89
let bs :: BS.ByteString
bs = "I'm a ByteString!"
putStrLn (toString bs)
-- => I'm a ByteString!
let text :: T.Text
text = "I'm a Text!"
putStrLn (toString text)
-- => I'm a Text!
10
'd'
MyJust 1.89
I'm a ByteString!
I'm a Text!