Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell aeson解码需要类型签名?_Haskell_Aeson - Fatal编程技术网

Haskell aeson解码需要类型签名?

Haskell aeson解码需要类型签名?,haskell,aeson,Haskell,Aeson,我正在编写一个小程序来解析来自TCP连接的JSON,但我遇到了一个错误: Main.hs:43:22: No instance for (FromJSON t0) arising from a use of `decode' The type variable `t0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there is a

我正在编写一个小程序来解析来自TCP连接的JSON,但我遇到了一个错误:

Main.hs:43:22:
    No instance for (FromJSON t0) arising from a use of `decode'
    The type variable `t0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there is a potential instance available:
      instance FromJSON LogIn -- Defined at Main.hs:21:10
    In the expression: decode lazyLine
    In a pattern binding: Just login = decode lazyLine
    In the expression:
      do { line <- BS.hGetLine handle;
           let lazyLine = BL.fromChunks ...;
           let Just login = decode lazyLine;
           hPutStrLn handle ".";
           .... }

如果您想让Aeson为您生成实例,请确保在编译中启用扩展DeriveGenerics

您需要向该行添加注释

改变

let Just login = decode lazyLine 

否则,您可以手动编写实例

您需要为
FromJSON登录
提供一个实现了
parseJSON
的typeclass实例

比如:

{-#语言重载字符串}
实例FromJSON坐标,其中
parseJSON(objectob)=登录
ob.:“用户名”
ob.:“密码”
parseJSON=mzero

如果您想让Aeson为您生成实例,请确保在编译中打开扩展DeriveGenerics

您需要向该行添加注释

改变

let Just login = decode lazyLine 

否则,您可以手动编写实例

您需要为
FromJSON登录
提供一个实现了
parseJSON
的typeclass实例

比如:

{-#语言重载字符串}
实例FromJSON坐标,其中
parseJSON(objectob)=登录
ob.:“用户名”
ob.:“密码”
parseJSON=mzero
这将编译:

commandProcessor :: Handle -> IO ()
commandProcessor handle = do
    line <- BS.hGetLine handle
    let lazyLine = BL.fromChunks [line]
    let Just login = decode lazyLine :: Maybe LogIn
    hPutStrLn handle "." --(show login)
    commandProcessor handle
这将汇编:

commandProcessor :: Handle -> IO ()
commandProcessor handle = do
    line <- BS.hGetLine handle
    let lazyLine = BL.fromChunks [line]
    let Just login = decode lazyLine :: Maybe LogIn
    hPutStrLn handle "." --(show login)
    commandProcessor handle

我正试图让埃森为我做这项工作,如-见“结构化”。我认为这是一个简单的单子问题,因为当我看到一些东西时,警告都变成了
无法将预期类型“IO BL.ByteString”与实际类型“BL.ByteString”
匹配。我正试图让埃森为我做这项工作,如-见“结构化”。我认为这是一个简单的monad问题,因为当我看到一些东西时,警告都变成了关于
无法将预期类型“IO BL.ByteString”与实际类型“BL.ByteString”匹配“
。谢谢,这就解决了问题。出于好奇,懒散是什么类型的?将其声明为
BL.ByteString
IO BL.ByteString
都会失败并出现错误。
lazyline
是一个懒惰的ByteString。它的实际类型是
Data.ByteString.Internal.Lazy
。值构造函数是隐藏的,因此需要使用类似于
fromChunks
的函数来创建lazy bytestring类型的值。要了解更多详细信息,只需在
ghci
中的
decode
功能上运行
:t
。感谢您的帮助,非常感谢。谢谢,这可以解决问题。出于好奇,懒散是什么类型的?将其声明为
BL.ByteString
IO BL.ByteString
都会失败并出现错误。
lazyline
是一个懒惰的ByteString。它的实际类型是
Data.ByteString.Internal.Lazy
。值构造函数是隐藏的,因此需要使用类似于
fromChunks
的函数来创建lazy bytestring类型的值。要了解更多详情,只需在
ghci
中的
decode
功能上运行
:t
,谢谢您的帮助,非常感谢。
commandProcessor :: Handle -> IO ()
commandProcessor handle = do
    line <- BS.hGetLine handle
    let lazyLine = BL.fromChunks [line]
    let Just login = decode lazyLine :: Maybe LogIn
    hPutStrLn handle "." --(show login)
    commandProcessor handle
let Just login = decode lazyLine :: Maybe LogIn