无法将预期类型`Data.ByteString.Internal.ByteString'与实际类型`ByteString'匹配

hug*_*omg 19 haskell bytestring

运行以下代码:

import Crypto.BCrypt
import Data.ByteString.Lazy.Char8

main = do
  maybe_pwhash <- hashPasswordUsingPolicy slowerBcryptHashingPolicy (pack "hunter2")
  print $ maybe_pwhash
Run Code Online (Sandbox Code Playgroud)

我收到以下编译错误:

test.hs:5:70:
    Couldn't match expected type `Data.ByteString.Internal.ByteString'
                with actual type `ByteString'
    In the return type of a call of `pack'
    In the second argument of `hashPasswordUsingPolicy', namely
      `(C.pack "hunter2")'
    In a stmt of a 'do' block:
      maybe_pwhash <- hashPasswordUsingPolicy
                        slowerBcryptHashingPolicy (pack "hunter2")
Run Code Online (Sandbox Code Playgroud)

我很困惑,因为我不明白为什么a Data.ByteString.Internal.ByteString和a 之间有区别ByteString.

hug*_*omg 9

根据bcrypt文档,你应该使用严格的字节串

import Data.ByteString.Char8
Run Code Online (Sandbox Code Playgroud)

而不是懒惰的:

import Data.ByteString.Lazy.Char8
Run Code Online (Sandbox Code Playgroud)

  • 澄清:有两种`ByteString`s:懒惰和严格.`Data.ByteString.(内部.)ByteString`是严格的.`Data.ByteString.Lazy.(内部.)ByteString`是懒惰的.如果你看到一个普通的旧的不合格的`ByteString`类型与一个合格的`ByteString`类型不匹配,那么它就是另一个. (6认同)
  • `.Char8`是邪恶/破碎/专家.你不应该使用它,除非你确切地知道它做了什么以及它有什么问题.ByteStrings是字节序列,而不是字符序列.话虽如此,`Data.ByteString.Char8.ByteString`与strict`ByteString`相同,`Data.ByteString.Lazy.Char8.ByteString`与lazy`ByteString`相同.只有两个`ByteString'. (6认同)