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

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

问题描述

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



我的问题:是否有一种方法可以在两种字符串类型之间进行自动转换,以避免编写 pack unpack 常常是这样吗?



在Python或JavaScript等其他编程语言中,例如在整数和浮点数之间自动转换if这是必需的。我也可以在Haskell中获得这样的东西吗?我知道,上面提到的语言是弱类型的,但我听说C ++有类似的功能。



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



扩展问题: <一个href =https://stackoverflow.com/questions/7357775/haskell-text-or-bytestring> Haskell。 Text或Bytestring 也涵盖了 Data.Text Data.ByteString 之间的区别。有没有办法在三个字符串 String Data.Text Data.ByteString ?

解决方案

否。

由于技术,哲学和几乎宗教方面的原因,Haskell没有隐式强制。



作为一个评论,在这些表示之间转换不是免费,大多数人不喜欢这样的想法,即隐藏着潜在的昂贵计算并且可能潜藏在其中。另外,将字符串当作懒惰列表,将它们强制转换为 Text 值可能不会终止。



文本 s自动通过 OverloadedString s来解析字符串文字foo to fromStringfoo fromString for Text 只是调用 pack



这个问题可能会问你为什么强迫这么多?是否有一些为什么你经常需要 unpack Text 值?如果你不断地将它们改为字符串,它就会失去目的。


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.

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?

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.

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.

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?

解决方案

No.

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

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.

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.

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天全站免登陆