haskell – ByteString到惰性文本,反之亦然

haskell – ByteString到惰性文本,反之亦然,第1张

概述将ByteString转换为Text时出现问题,反之亦然.这是代码: {-# LANGUAGE OverloadedStrings #-}import Web.Scottyimport Web.ClientSessionimport Data.Text.Lazy (Text, toStrict, fromStrict)import Data.Text.Lazy.Encoding (enc 将ByteString转换为Text时出现问题,反之亦然.这是代码:

{-# LANGUAGE OverloadedStrings #-}import Web.Scottyimport Web.ClIEntSessionimport 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 -> ByteStringencode = encodeUtf8 . toStrictdecode :: ByteString -> Textdecode = fromStrict . decodeUtf8

和错误消息:

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 <> ";"'

所以,我猜这个错误与ClIEntSession实际使用的内容有关,在他们的源代码中他们似乎使用普通的bytestring,它应该与我的实现一起使用.看这里:

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

那么为什么我会得到所有这些错误?谢谢.

解决方法 您正在混合Data.ByteString.ByteString和Data.ByteString.Lazy.ByteString.因为类型名称相等,GHC可以(并且确实)产生可怕的错误消息.我使用ByteString和Text的显式导入重新编写它,希望它现在更加明显:

{-# LANGUAGE OverloadedStrings #-}import Web.Scottyimport Web.ClIEntSessionimport Control.Monad.Trans (liftIO)import qualifIEd Data.Text as Timport qualifIEd Data.Text.EnCoding as Timport qualifIEd Data.Text.Lazy as TLimport qualifIEd Data.Text.Lazy.EnCoding as TLimport qualifIEd Data.ByteString as Bimport qualifIEd Data.ByteString as BLimport 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.ByteStringencode = T.encodeUtf8 . TL.toStrictdecode :: B.ByteString -> TL.Textdecode = TL.fromStrict . T.decodeUtf8
总结

以上是内存溢出为你收集整理的haskell – ByteString到惰性文本,反之亦然全部内容,希望文章能够帮你解决haskell – ByteString到惰性文本,反之亦然所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1086005.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存