0

I use encode (Haskell type via JSON to a string):

import GHC.Generics
import Data.Aeson

The string is (according to an error message from the compiler): Data.ByteString.Lazy.Internal.ByteString.

How do I putStrLn it?

put theoretically

What I'm in search for is a an ad-hoc polymorphic putStrLn, i.e., I'm looking for the proper instance of putStrLn semantics for a specific string-like type.

compiler messages

The compiler message if someone is interested:

valencies.lhs:182:22:
    Couldn't match type `Data.ByteString.Lazy.Internal.ByteString'
                  with `[Char]'
    Expected type: String
      Actual type: Data.ByteString.Lazy.Internal.ByteString
    In the return type of a call of `encode'
    In the first argument of `putStrLn', namely `(encode CALL)'
    In the expression: putStrLn (encode CALL)

see also

Similar question, but with a bit different type: How do I putStrLn a Data.ByteString.Internal.ByteString?.

Community
  • 1
  • 1
imz -- Ivan Zakharyaschev
  • 4,921
  • 6
  • 53
  • 104

3 Answers3

2

This really depends on your ByteString. Does it really contain printable characters? What's the encoding on it? You can use toString from utf8-string if you know it contains valid UTF-8 and then pass it to putStrLn.

By the way, Hayoo is great for this type (hehe) of questions. Put a type in there and it will get you functions from Hackage with those types!

Juan Pablo Santos
  • 1,200
  • 2
  • 10
  • 25
2

You may use string-class package, it has three options: toString function (defaults to utf-8), fromLazyByteString function, and putStrLn that works with all the types. Note, that you need to do:

import Prelude hiding (putStrLn)
import Data.String.Class (putStrLn)
Konstantine Rybnikov
  • 2,457
  • 1
  • 22
  • 29
  • That's smart! So, with `string-class` I don't have to explicitly choose the corresponding variant every time I write `putStrLn` , as in http://stackoverflow.com/a/28500229/94687 ?.. – imz -- Ivan Zakharyaschev Feb 17 '15 at 11:16
  • I had read about [-XOverloadedStrings](https://downloads.haskell.org/~ghc/7.8.2/docs/html/users_guide/type-class-extensions.html#overloaded-strings). string-class is like taking the idea of `IsString` class further: with the `StringCells` and `StringRWIO` classes whose instances hold the dictionary of standard string functions and string IO actions for other string-like types.... – imz -- Ivan Zakharyaschev Feb 17 '15 at 12:14
0

In this case, we have a Lazy variant, so we must use Data.ByteString.Lazy.putStrLn.

Note that the compiler gives an interesting warning:

In the use of `Data.ByteString.Lazy.putStrLn'
(imported from Data.ByteString.Lazy):
Deprecated: "Use Data.ByteString.Lazy.Char8.putStrLn instead. (Functions that rely on ASCII encodings belong in Data.ByteString.Lazy.Char8)"

Probably, if I want UTF-8 encoded strings, I should use another variant rather than the recommended Char8 one. I'll try to find out this.

smart string-class

For a smart way, also have a look at string-class package mentioned in another answer by Konstantine Rybnikov.

Community
  • 1
  • 1
imz -- Ivan Zakharyaschev
  • 4,921
  • 6
  • 53
  • 104