https://github.com/nwtgck/to-string-haskell
toString in Haskell
https://github.com/nwtgck/to-string-haskell
haskell haskell-library haskell-stack
Last synced: 9 months ago
JSON representation
toString in Haskell
- Host: GitHub
- URL: https://github.com/nwtgck/to-string-haskell
- Owner: nwtgck
- License: bsd-3-clause
- Created: 2018-03-25T04:07:40.000Z (over 7 years ago)
- Default Branch: develop
- Last Pushed: 2018-03-25T07:19:34.000Z (over 7 years ago)
- Last Synced: 2025-02-06T13:29:53.349Z (10 months ago)
- Topics: haskell, haskell-library, haskell-stack
- Language: Haskell
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# to-string
A `toString` converter for String-like types and any `Show a => a` type.
| branch | Travis status|
| --- | --- |
| [`master`](https://github.com/nwtgck/to-string-haskell/tree/master) | [](https://travis-ci.org/nwtgck/to-string-haskell) |
| [`develop`](https://github.com/nwtgck/to-string-haskell/tree/develop) | [](https://travis-ci.org/nwtgck/to-string-haskell) |
## Installation
Add this library to `extra-deps` in your `stack.yaml` like the following if you use [Stack](https://docs.haskellstack.org/en/stable/README/).
```yaml
...
extra-deps:
- git: https://github.com/nwtgck/to-string-haskell.git
commit: 0c8f2951606e185feacddb28983b30c527c3eb17
...
```
## Usages
```hs
toString 89 == "89"
```
```hs
toString "hello" == "hello"
```
```hs
{-# LANGUAGE OverloadedStrings #-}
toString ("I'm a ByteString" :: BS.ByteString) == ("I'm a ByteString" :: String)
```
```hs
{-# LANGUAGE OverloadedStrings #-}
toString ("I'm a Text" :: T.Text) == ("I'm a Text" :: String)
```
## Supported String-like types
* `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`
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`
## Executable example
```hs
{-# 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!
```
### Output
```txt
10
'd'
MyJust 1.89
I'm a ByteString!
I'm a Text!
```