ByteString for lazy text and vice versa

I'm having trouble turning ByteString into text and vice versa. Here is the code:

{-# LANGUAGE OverloadedStrings #-} import Web.Scotty import Web.ClientSession import Data.Text.Lazy (Text, toStrict, fromStrict) import Data.Text.Lazy.Encoding (encodeUtf8, decodeUtf8) import Data.ByteString (ByteString) import Data.Monoid ((<>)) initCookies :: IO (Text -> ActionM ()) initCookies = do key <- getDefaultKey return $ setCookie key where setCookie k id = encryptIO k (encode id) >>= (\x -> header "Set-Cookie" ("sid=" <> decode x <> ";")) encode :: Text -> ByteString encode = encodeUtf8 . toStrict decode :: ByteString -> Text decode = fromStrict . decodeUtf8 

and error message:

 foo.hs:16:35: Couldn't match expected type `bytestring-0.10.0.2:Data.ByteString.Internal.ByteString' with actual type `ByteString' In the return type of a call of `encode' In the second argument of `encryptIO', namely `(encode id)' In the first argument of `(>>=)', namely `encryptIO k (encode id)' foo.hs:17:18: Couldn't match type `ActionM' with `IO' Expected type: IO () Actual type: ActionM () In the return type of a call of `header' In the expression: header "Set-Cookie" ("sid=" <> decode x <> ";") In the second argument of `(>>=)', namely `(\ x -> header "Set-Cookie" ("sid=" <> decode x <> ";"))' foo.hs:17:56: Couldn't match expected type `ByteString' with actual type `bytestring-0.10.0.2:Data.ByteString.Internal.ByteString' In the first argument of `decode', namely `x' In the first argument of `(<>)', namely `decode x' In the second argument of `(<>)', namely `decode x <> ";"' 

So, I think this error has something to do with what ClientSession uses, in their source code they seem to use the usual bytestring, which should work with my implementation. Look here:

 [..] import qualified Data.ByteString as S [..] encryptIO :: Key -> S.ByteString -> IO S.ByteString [..] 

So why am I getting all these errors? Thanks.

+4
source share
2 answers

You mixed Data.ByteString.ByteString and Data.ByteString.Lazy.ByteString . Since the type names are equal, the GHC can (and does) create horrible error messages. I reworked it using explicit import for ByteString and Text , hopefully it will become more obvious now:

 {-# LANGUAGE OverloadedStrings #-} import Web.Scotty import Web.ClientSession import Control.Monad.Trans (liftIO) import qualified Data.Text as T import qualified Data.Text.Encoding as T import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy.Encoding as TL import qualified Data.ByteString as B import qualified Data.ByteString as BL import Data.Monoid ((<>)) initCookies :: IO (TL.Text -> ActionM ()) initCookies = do key <- getDefaultKey return $ setCookie key where setCookie k id = liftIO (encryptIO k (encode id)) >>= (\x -> header "Set-Cookie" ("sid=" <> decode x <> ";")) encode :: TL.Text -> B.ByteString encode = T.encodeUtf8 . TL.toStrict decode :: B.ByteString -> TL.Text decode = TL.fromStrict . T.decodeUtf8 
+6
source

Data.String.Conversions contains theses using a single cs conversion function, which is called depending on the context of the call (i.e., input and expected type).

+1
source

Source: https://habr.com/ru/post/1501868/


All Articles