universum icon indicating copy to clipboard operation
universum copied to clipboard

Do something with 'fromStrict' and 'toStrict'

Open chshersh opened this issue 6 years ago • 5 comments

Those functions can be polymorphic. Also we can add fromLazy and toLazy for convenience. Also either deprecate toLText or add toLByteString or only add toLByteString and remove fromStrict and toStrict from exports.

Initially was raised by @gromakovsky.

I tried to convert from Text to LByteString with universum and was really confused... The only way to do this is to write:

import qualified Data.ByteString.Lazy as BSL (fromStrict)

t2b :: Text -> LByteString
t2b = BSL.fromStrict . encodeUtf8

Discuss please.

chshersh avatar May 11 '18 07:05 chshersh

class LazyStrict l s | l -> s, s -> l where
  toLazy :: s -> l
  toStrict :: l -> s

fromLazy :: LazyStrict l s => l -> s
fromLazy = toStrict

fromStrict :: LazyStrict l s => s -> l
fromStrict = toLazy

instance LazyStrict L.ByteString S.ByteString
instance LazyStrict L.Text S.Text

something like this?

int-index avatar May 11 '18 07:05 int-index

@int-index Yes, this looks quite good to me.

chshersh avatar May 11 '18 08:05 chshersh

t2b :: Text -> LByteString
t2b = BSL.fromStrict . encodeUtf8

It's strange that you need to do BSL.fromStrict, because encodeUtf8 is polymorphic and there is instance ConvertUtf8 Text LByteString.

gromakovsky avatar May 11 '18 18:05 gromakovsky

Also either deprecate toLText or add toLByteString or only add toLByteString

I say we deprecate and eventually delete toLText

dcastro avatar Jul 10 '22 18:07 dcastro

Let's also:

  • Delete the re-export of Data.Text.Lazy.toStrict and Data.Text.Lazy.fromStrict from Universum.String.Reexport
  • Remember to update the rules in .hlint.yaml

dcastro avatar Sep 10 '22 09:09 dcastro