[英]How do I putStrLn a Data.ByteString.Lazy.Internal.ByteString?
I use encode
(Haskell type via JSON to a string): 我使用
encode
(通过JSON将Haskell类型转换为字符串):
import GHC.Generics
import Data.Aeson
The string is (according to an error message from the compiler): Data.ByteString.Lazy.Internal.ByteString
. 该字符串是(根据来自编译器的错误消息):
Data.ByteString.Lazy.Internal.ByteString
。
How do I putStrLn it? 我该如何放置?
What I'm in search for is a an ad-hoc polymorphic putStrLn
, ie, I'm looking for the proper instance of putStrLn
semantics for a specific string-like type. 我要寻找的是临时多态的
putStrLn
,即,我正在为特定的类似字符串的类型寻找适当的putStrLn
语义实例。
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)
Similar question, but with a bit different type: How do I putStrLn a Data.ByteString.Internal.ByteString? 类似的问题,但类型有所不同: 如何将StrutLn设置为Data.ByteString.Internal.ByteString? .
。
This really depends on your ByteString
. 这确实取决于您的
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
. 如果您知道它包含有效的UTF-8,则可以使用
utf8-string
toString
,然后将其传递给putStrLn
。
By the way, Hayoo is great for this type (hehe) of questions. 顺便说一句, Hayoo非常适合此类问题。 Put a type in there and it will get you functions from Hackage with those types!
在其中放置一个类型,它将为您提供来自Hackage的具有这些类型的功能!
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. 您可以使用字符串类包,它具有三个选项:
toString
函数(默认为utf-8), fromLazyByteString
函数以及与所有类型都putStrLn
。 Note, that you need to do: 请注意,您需要执行以下操作:
import Prelude hiding (putStrLn)
import Data.String.Class (putStrLn)
In this case, we have a Lazy
variant, so we must use Data.ByteString.Lazy.putStrLn
. 在这种情况下,我们有一个
Lazy
变体,因此我们必须使用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. 可能的是,如果我想要UTF-8编码的字符串,则应该使用其他变体而不是推荐的
Char8
。 I'll try to find out this. 我将尝试找出答案。
For a smart way, also have a look at string-class
package mentioned in another answer by Konstantine Rybnikov . 作为一种聪明的方法,还可以看看Konstantine Rybnikov的另一个答案中提到的
string-class
包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.