Haskell中String和Data.Text之间的自动转换 [英] Automatic conversion between String and Data.Text in haskell

查看:21
本文介绍了Haskell中String和Data.Text之间的自动转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如 Nikita Volkov 在他的问题 Data.Text vs String 中提到的那样,我也想知道为什么我必须处理haskell中不同的String实现type String = [Char]Data.Text.在我的代码中,我经常使用 packunpack 函数.

As Nikita Volkov mentioned in his question Data.Text vs String I also wondered why I have to deal with the different String implementations type String = [Char] and Data.Text in haskell. In my code I use the pack and unpack functions really often.

我的问题:有没有办法在两种字符串类型之间进行自动转换,这样我就可以避免编写 packunpack 所以经常?

My question: Is there a way to have an automatic conversion between both string types so that I can avoid writing pack and unpack so often?

在 Python 或 JavaScript 等其他编程语言中,例如,如果需要,可以在整数和浮点数之间进行自动转换.我也可以在haskell中达到类似的效果吗?我知道,上面提到的语言是弱类型的,但我听说 C++ 也有类似的特性.

In other programming languages like Python or JavaScript there is for example an automatic conversion between integers and floats if it is needed. Can I reach something like this also in haskell? I know, that the mentioned languages are weakly typed, but I heard that C++ has a similar feature.

注意:我已经知道语言扩展 {-# LANGUAGE OverloadedStrings #-}.但据我了解,这种语言扩展只适用于定义为 "..." 的字符串.我想对从其他函数或函数定义中作为参数的字符串进行自动转换.

Note: I already know the language extension {-# LANGUAGE OverloadedStrings #-}. But as I understand this language extensions just applies to strings defined as "...". I want to have an automatic conversion for strings which I got from other functions or I have as arguments in function definitions.

扩展问题: Haskell.Text 或 Bytestring 也涵盖了 Data.TextData.ByteString 之间的区别.有没有办法在 StringData.TextData.ByteString 三个字符串之间进行自动转换?

Extended question: Haskell. Text or Bytestring covers also the difference between Data.Text and Data.ByteString. Is there a way to have an automatic conversion between the three strings String, Data.Text and Data.ByteString?

推荐答案

没有.

Haskell 没有出于技术、哲学和几乎宗教原因的隐含强制.

Haskell doesn't have implicit coercions for technical, philosophical, and almost religious reasons.

作为评论,在这些表示之间进行转换 并不是免费的,而且大多数人不喜欢你隐藏着潜在的昂贵计算的想法.此外,将字符串作为惰性列表,将它们强制为 Text 值可能不会终止.

As a comment, converting between these representations isn't free and most people don't like the idea that you have hidden and potentially expensive computations lurking around. Additionally, with strings as lazy lists, coercing them to a Text value might not terminate.

我们可以通过将字符串文字 "foo" 脱糖到 fromString "foo 来自动将文字转换为 TextsText 的 "fromString 只是调用 pack.

We can convert literals to Texts automatically with OverloadedStrings by desugaring a string literal "foo" to fromString "foo" and fromString for Text just calls pack.

问题可能是问你为什么要强迫这么多?是否有一些你需要经常unpack Text 值的原因?如果您不断将它们更改为字符串,则会有点违背目的.

The question might be to ask why you're coercing so much? Is there some why do you need to unpack Text values so often? If you constantly changing them to strings it defeats the purpose a bit.

这篇关于Haskell中String和Data.Text之间的自动转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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