Haskell:打印文本编码

发布于 2024-09-10 22:37:59 字数 682 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几度春秋 2024-09-17 22:37:59

要在 Haskell 中打印某种类型的值,该类型必须是 Show 类的实例。

localeEncoding :: TextEncoding

并且 TextEncoding 不是 Show 的实例。

TextEncoding 类型实际上是一种存在类型,存储编码和解码方法:

data TextEncoding
  = forall dstate estate . TextEncoding  {
    mkTextDecoder :: IO (TextDecoder dstate),
    mkTextEncoder :: IO (TextEncoder estate)
  }

由于这些是函数,因此没有明智的方式来显示它们。当前的 localeEncoding 是通过 C 函数 nl_langinfo 使用 iconv 确定的。

因此,TextEncoding 本身不是可显示的类型,因此您无法打印它。但是,您可以通过 mkTextEncoding 构造此类型的新值。例如,创建一个 utf8 环境:

mkTextEncoding "UTF-8" 

我们可能会考虑使用 TextEncoding 存储区域设置表示的功能请求,以便可以打印该标签。然而,目前这是不可能的。

To print a value of some type in Haskell, the type must be an instance of the Show class.

localeEncoding :: TextEncoding

and TextEncoding is not an instance of Show.

The TextEncoding type is actually an existential type storing the methods for encoding and decoding:

data TextEncoding
  = forall dstate estate . TextEncoding  {
    mkTextDecoder :: IO (TextDecoder dstate),
    mkTextEncoder :: IO (TextEncoder estate)
  }

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:

mkTextEncoding "UTF-8" 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文