我使用encode (Haskell type通过JSON转换为字符串):
import GHC.Generics
import Data.Aeson字符串是(根据来自编译器的错误消息):Data.ByteString.Lazy.Internal.ByteString。
我如何putStrLn它?
从理论上讲
我正在寻找的是一个即席的多态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)另请参阅
类似的问题,但类型略有不同:How do I putStrLn a Data.ByteString.Internal.ByteString?。
发布于 2015-02-13 22:58:05
这真的取决于你的ByteString。它真的包含可打印的字符吗?它的编码是什么?如果知道toString包含有效的UTF-8,则可以从utf8-string使用它,然后将其传递给putStrLn。
顺便说一句,Hayoo非常适合这类问题(哈哈)。在其中放入一个类型,它会用这些类型从Hackage中获取函数!
发布于 2015-02-14 02:14:16
你可以使用string-class包,它有三个选项:toString函数(默认为utf-8),fromLazyByteString函数,以及支持所有类型的putStrLn。请注意,您需要执行以下操作:
import Prelude hiding (putStrLn)
import Data.String.Class (putStrLn)发布于 2015-02-13 21:01:42
在本例中,我们有一个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)"也许,如果我想要UTF8编码的字符串,我应该使用另一个变体,而不是推荐的Char8变体。我会试着找出这个。
智能字符串类
更聪明的方法是,也可以看看another answer by Konstantine Rybnikov中提到的string-class包。
https://stackoverflow.com/questions/28500006
复制相似问题