Mat*_*ner 3 haskell type-conversion bytestring overloaded-strings
我将(严格)传递ByteString
给期望a的东西System.IO.FilePath
,声明为type FilePath = String
.我也在用{-# LANGUAGE OverloadedStrings #-}
.我在某些地方的转换是自动发生的,但在这里却没有.我有什么问题?
Main.hs:33:40: error:
• Couldn't match type ‘ByteString’ with ‘[Char]’
Expected type: FilePath
Actual type: ByteString
Run Code Online (Sandbox Code Playgroud)
Wil*_*sem 12
该{-# LANGUAGE OverloadedStrings #-}
pragma仅适用于字符串文字,例如"a string"
.在这种情况下,哈斯克尔隐含放置一个fromString
每个字符串文字之前,所以它重写一个字符串字面作为"a string"
对fromString "a string"
.这只发生在文字上.
在Haskell中,据我所知,没有隐式转换.例如Int
,之间的转换Float
都是明确的.
另外请注意,IsString
类型类只有一个函数fromString :: String -> a
.所以这意味着它只能从字符串到该实例(这里ByteString
),而不是相反.
您可以使用unpack :: ByteString -> String
转换ByteString
为a String
.