Automatic conversion between String and Data.Text in haskell

前端 未结 2 1109
感动是毒
感动是毒 2020-12-14 00:53

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]

2条回答
  •  一整个雨季
    2020-12-14 01:17

    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.

提交回复
热议问题