Haskell:当我使用getArgs时ByteString问题

时间:2015-12-21 15:25:43

标签: haskell cabal-sandbox

我正在使用ghc 7.10.3在沙盒中工作,我有这个简单的代码:

main :: IO ()
main = do
  args <- getArgs
  case args of
    [ newOauthConsumerKey, newOauthConsumerSecret ] -> do
      let appOauth = newOAuth { oauthServerName       = "api.twitter.com"
                              , oauthConsumerKey    = newOauthConsumerKey
                              , oauthConsumerSecret = newOauthConsumerSecret
                              }
  ... 

当我在我的cabal文件中使用这些依赖项编译它时:

base
, authenticate-oauth
, http-conduit
, aeson
, text

我继续得到(在清理和重建沙箱之后)以下错误:

src/Test/Main.hs:60:55:
    Couldn't match type ‘[Char]’
                   with ‘bytestring-0.10.6.0:Data.ByteString.Internal.ByteString’
    Expected type: bytestring-0.10.6.0:Data.ByteString.Internal.ByteString
      Actual type: String
    In the ‘oauthConsumerKey’ field of a record
    In the expression:
      newOAuth
        {oauthServerName = "api.twitter.com",
         oauthConsumerKey = newOauthConsumerKey,
         oauthConsumerSecret = newOauthConsumerSecret}

src/Test/Main.hs:61:55:
    Couldn't match type ‘[Char]’
                   with ‘bytestring-0.10.6.0:Data.ByteString.Internal.ByteString’
    Expected type: bytestring-0.10.6.0:Data.ByteString.Internal.ByteString
      Actual type: String
    In the ‘oauthConsumerSecret’ field of a record
    In the expression:
      newOAuth
        {oauthServerName = "api.twitter.com",
         oauthConsumerKey = newOauthConsumerKey,
         oauthConsumerSecret = newOauthConsumerSecret}

我已经使用Google搜索并检查了不同类似的答案,但我无法使其正常工作。有什么想法吗?

解决方案

问题原因在于代码中import Data.Text的导入包含pack,与pack所需的Data.ByteString.Char8不同。一旦我加入它,它就有用了。

1 个答案:

答案 0 :(得分:3)

字段oauthConsumerKeyoauthConsumerSecret应该包含ByteString s而不是String s,这是getArgs返回的内容。您需要使用pack转换newOauthConsumer*值。