Haskell:打印文本编码
Haskell 新手在这里。
$ ghc --version The Glorious Glasgow Haskell Compilation System, version 6.12.1
在尝试调试第三方 Haskell 程序中与区域设置相关的奇怪错误时,我尝试打印默认编码:
import System.IO
main = do
print localeEncoding
但它失败了:
$ ghc -o printlocale main.hs main.hs:4:2: No instance for (Show TextEncoding) arising from a use of `print' at main.hs:4:2-21 Possible fix: add an instance declaration for (Show TextEncoding) In the expression: print localeEncoding In the expression: do { print localeEncoding } In the definition of `main': main = do { print localeEncoding }
My google-fu is failed me。我缺少什么?
Haskell newbie here.
$ ghc --version The Glorious Glasgow Haskell Compilation System, version 6.12.1
While trying to debug weird locale-related bug in third-party Haskell program, I'm trying to print default encoding:
import System.IO
main = do
print localeEncoding
But it fails:
$ ghc -o printlocale main.hs main.hs:4:2: No instance for (Show TextEncoding) arising from a use of `print' at main.hs:4:2-21 Possible fix: add an instance declaration for (Show TextEncoding) In the expression: print localeEncoding In the expression: do { print localeEncoding } In the definition of `main': main = do { print localeEncoding }
My google-fu is failing me. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在 Haskell 中打印某种类型的值,该类型必须是 Show 类的实例。
并且 TextEncoding 不是 Show 的实例。
TextEncoding 类型实际上是一种存在类型,存储编码和解码方法:
由于这些是函数,因此没有明智的方式来显示它们。当前的 localeEncoding 是通过 C 函数 nl_langinfo 使用 iconv 确定的。
因此,TextEncoding 本身不是可显示的类型,因此您无法打印它。但是,您可以通过 mkTextEncoding 构造此类型的新值。例如,创建一个 utf8 环境:
我们可能会考虑使用 TextEncoding 存储区域设置表示的功能请求,以便可以打印该标签。然而,目前这是不可能的。
To print a value of some type in Haskell, the type must be an instance of the Show class.
and TextEncoding is not an instance of Show.
The TextEncoding type is actually an existential type storing the methods for encoding and decoding:
Since these are functions, there's no sensible way to show them. The current localeEncoding is determined using iconv, via the C function nl_langinfo.
So, TextEncoding as such is not a showable type, so you cannot print it. However, you can construct new values of this type, via mkTextEncoding. E.g. to create a utf8 environment:
We might consider a feature request to store the representation of the locale with the TextEncoding, so this label could be printed. However, that's currently not possible.