Data.Text与字符串 [英] Data.Text vs String

查看:135
本文介绍了Data.Text与字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管Haskell社区的一般观点似乎是,最好使用 Text 而不是 String ,大多数维护的库的API仍然是 String 的事实混淆了我。另一方面,着名的项目,其中考虑字符串作为一个错误,并提供一个 Prelude ,其中所有字符串函数实例替换为<$ c

那么是否有任何理由让人们继续写 String 字符串

$ b $相比, Text 可能存在其他任何缺点b

特别是,我对此感兴趣,因为我正在设计一个库,并试图决定使用哪种类型来表示错误消息。

解决方案

我的不合格猜测是,大多数库编写者不希望添加更多的依赖关系。由于字符串是字面上每个Haskell发行版的一部分(它是语言标准的一部分!),如果使用字符串并且不要求用户通过hackage整理文本发行版,则更容易获得采用。



这是您必须忍受的那些设计错误之一,除非您能说服大部分社区转换过夜。只要看看让Applicative成为Monad的超类所花费的时间 - 这是一个相对较小但很想要的变化 - 并且想象用Text替换所有String事物需要多长时间。






要回答您更具体的问题:除非您通过使用文本获得明显的性能优势,否则我会使用String。错误消息通常是一些小事情,所以使用String应该不是什么大问题。另一方面,如果你是那种意识形态的人纯粹主义者避免用实用主义为理想主义,用文本去。






*我将恐怖报价中的设计错误归因于字符串作为列表字符是一个整洁的属性,使他们很容易推理并与其他现有的列表操作函数集成。


While the general opinion of the Haskell community seems to be that it's always better to use Text instead of String, the fact that still the APIs of most of maintained libraries are String-oriented confuses the hell out of me. On the other hand, there are notable projects, which consider String as a mistake altogether and provide a Prelude with all instances of String-oriented functions replaced with their Text-counterparts.

So are there any reasons for people to keep writing String-oriented APIs except backwards- and standard Prelude-compatibility and the "switch-making intertia"? Are there possibly any other drawbacks to Text as compared to String?

Particularly, I'm interested in this because I'm designing a library and trying to decide which type to use to express error messages.

解决方案

My unqualified guess is that most library writers don't want to add more dependencies than necessary. Since strings are part of literally every Haskell distribution (it's part of the language standard!), it is a lot easier to get adopted if you use strings and don't require your users to sort out Text distributions from hackage.

It's one of those "design mistakes" that you just have to live with unless you can convince most of the community to switch over night. Just look at how long it has taken to get Applicative to be a superclass of Monad – a relatively minor but much wanted change – and imagine how long it would take to replace all the String things with Text.


To answer your more specific question: I would go with String unless you get noticeable performance benefits by using Text. Error messages are usually rather small one-off things so it shouldn't be a big problem to use String.

On the other hand, if you are the kind of ideological purist that eschews pragmatism for idealism, go with Text.


* I put design mistakes in scare quotes because strings as a list-of-chars is a neat property that makes them easy to reason about and integrate with other existing list-operating functions.

这篇关于Data.Text与字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆