【问题标题】:Haskell ByteString type errorHaskell ByteString 类型错误
【发布时间】:2015-02-06 14:06:49
【问题描述】:

这是我第一次使用 ByteStrings,也是我第一次看到 pcap 文件。 我基本上是在尝试有效地使用 ByteStrings 读取 pcap 文件并在屏幕上打印其内容。 我正在使用Network.Pcap library 来读取文件。 ByteString 变体可以在这里找到:Network.Pcap ByteString。为了方便起见,我只想打印文件的 first 行,所以我的代码如下所示:

1 import qualified Data.ByteString as B

2 printIt :: PktHdr -> B.ByteString -> IO ()
3 printIt ph bytep = do
4    print $ hdrCaptureLength ph  -- not important
5    print $ bytep

6 main = do
7    f <- openOffline "file.pcap"
8    dispatchBS f (1) printIt

其中printItcallbackBS 函数,对文件主体进行操作。

编译器报错:

Couldn't match type ‘B.ByteString’
              with ‘bytestring-0.10.4.0:Data.ByteString.Internal.ByteString’
NB: ‘B.ByteString’
      is defined in ‘Data.ByteString.Internal’
          in package ‘bytestring-0.10.4.1’
    ‘bytestring-0.10.4.0:Data.ByteString.Internal.ByteString’
      is defined in ‘Data.ByteString.Internal’
          in package ‘bytestring-0.10.4.0’
Expected type: CallbackBS
  Actual type: PktHdr -> B.ByteString -> IO ()
In the third argument of ‘dispatchBS’, namely ‘printIt’
In a stmt of a 'do' block: dispatchBS f (1) printIt

我的理解是,对于编译器,callbackBS 函数的类型必须是:PktHdr -&gt; ByteString -&gt; IO (),而在第 2 行,类型是PktHdr -&gt;B.@ 987654329@。 但是我不能简单地使用 ByteString 类型,因为那样我会与正常列表的前奏中定义的函数发生冲突。 你有什么想法吗?

【问题讨论】:

标签: haskell pcap bytestring


【解决方案1】:

编译器试图告诉您您正在使用两个不同的bytestring 包。有关详细信息和解决方案,请参见此处:"Couldn't match expected type with actual type" error when using Codec.BMP

【讨论】:

  • @Filippo 您可能应该修改您的代码以防止此类问题。例如见wiki.haskell.org/How_to_write_a_Haskell_program
  • 如果现在我使用 ByteStrings 的 Lazy 版本 (import qualified Data.ByteString.Lazy as B ),它不再起作用,并且我收到与以前类似的错误:Couldn't match type ‘B.ByteString’ with ‘Data.ByteString.Internal.ByteString’ NB: ‘B.ByteString’ is defined in ‘Data.ByteString.Lazy.Internal’ ‘Data.ByteString.Internal.ByteString’ is defined in ‘Data.ByteString.Internal’
  • 这是一个不同的错误。这是因为惰性和严格的字节串不是同一类型。您需要在它们之间进行转换。不久前我写了一个tutorial on bytestrings,你可能想看看。
  • 非常感谢,这真的很有帮助。不敢相信 FPComplete 的人正在回答我的愚蠢问题。再次感谢。
  • 不客气。不过,我不是 FPComplete 的人 :)。任何人都可以创建帐户并发布内容。您还可以通过点击右上角的小播放图标来就地运行代码,这很好。
猜你喜欢
  • 1970-01-01
  • 2011-02-08
  • 2013-09-04
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
相关资源
最近更新 更多