繁体   English   中英

如何将StrutLn设置为Data.ByteString.Lazy.Internal.ByteString?

[英]How do I putStrLn a Data.ByteString.Lazy.Internal.ByteString?

我使用encode (通过JSON将Haskell类型转换为字符串):

import GHC.Generics
import Data.Aeson

该字符串是(根据来自编译器的错误消息): Data.ByteString.Lazy.Internal.ByteString

我该如何放置?

从理论上讲

我要寻找的是临时多态的putStrLn ,即,我正在为特定的类似字符串的类型寻找适当的putStrLn语义实例。

编译器消息

如果有人感兴趣,编译器消息:

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)

也可以看看

类似的问题,但类型有所不同: 如何将StrutLn设置为Data.ByteString.Internal.ByteString?

这确实取决于您的ByteString 它真的包含可打印字符吗? 它的编码是什么? 如果您知道它包含有效的UTF-8,则可以使用utf8-string toString ,然后将其传递给putStrLn

顺便说一句, Hayoo非常适合此类问题。 在其中放置一个类型,它将为您提供来自Hackage的具有这些类型的功能!

您可以使用字符串类包,它具有三个选项: toString函数(默认为utf-8), fromLazyByteString函数以及与所有类型都putStrLn 请注意,您需要执行以下操作:

import Prelude hiding (putStrLn)
import Data.String.Class (putStrLn)

在这种情况下,我们有一个Lazy变体,因此我们必须使用Data.ByteString.Lazy.putStrLn

请注意,编译器给出了一个有趣的警告:

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)"

可能的是,如果我想要UTF-8编码的字符串,则应该使用其他变体而不是推荐的Char8 我将尝试找出答案。

智能字符串类

作为一种聪明的方法,还可以看看Konstantine Rybnikov的另一个答案中提到的string-class包。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM