本文介绍了Haskell:打印TextEncoding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这里有Haskell新手.

Haskell newbie here.


$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.12.1

当尝试调试第三方Haskell程序中与语言环境相关的怪异错误时,我正在尝试打印默认编码:

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

但是失败了:


$ 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 }

我的google-fu让我失望.我想念什么?

My google-fu is failing me. What am I missing?

推荐答案

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

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

localeEncoding :: TextEncoding

并且TextEncoding不是Show的实例.

and TextEncoding is not an instance of Show.

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

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

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

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.

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

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"

我们可能会考虑一个功能请求,以使用TextEncoding存储语言环境的表示形式,因此可以打印此标签.但是,目前这是不可能的.

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.

这篇关于Haskell:打印TextEncoding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-09 01:58