Json 不能';t将预期的FullWeather类型与实际的m1 FullWeather类型匹配
我正在使用json代码。由于类型中的错误,我无法从中提取值。为了得到这个错误,我使用了一个函数eitherDecode 代码部分:Json 不能';t将预期的FullWeather类型与实际的m1 FullWeather类型匹配,json,haskell,haskell-stack,Json,Haskell,Haskell Stack,我正在使用json代码。由于类型中的错误,我无法从中提取值。为了得到这个错误,我使用了一个函数eitherDecode 代码部分: extractValues :: BSL.ByteString -> FullWeather extractValues rawJSON = do let result = eitherDecode rawJSON :: Either String FullWeather case result of Left problem
extractValues :: BSL.ByteString -> FullWeather
extractValues rawJSON = do
let result = eitherDecode rawJSON :: Either String FullWeather
case result of
Left problem -> return problem
Right ok -> return ok
厄洛尔:
Couldn't match expected type ‘FullWeather’
with actual type ‘m0 String’
25 Left problem -> return problem
Couldn't match expected type ‘FullWeather’
with actual type ‘m1 FullWeather’
26 Right ok -> return ok
我可以发现三个问题:
(>>=)
和(>>)
应用程序的语法糖。不要对不返回一元值的函数使用do符号(如IO a
,[a]
,可能是a
…)。在do符号之外,正确的语法是let。。。在…
我建议遵循Haskell教程,如,它将教您正确使用do符号
return
的类型为return::Monad m=>a->ma
返回
不是关键字FullWeather
,它也不能返回字符串。这就是或可能的用途。
你可以做的另一件事是抛出一个错误
extractValues :: BSL.ByteString -> FullWeather
extractValues rawJSON =
let result = eitherDecode rawJSON :: Either String FullWeather
in case result of
Left problem -> error problem
Right ok -> ok
可能是FullWeather
而不是FullWeather
extractValues :: BSL.ByteString -> Maybe FullWeather
extractValues rawJSON =
let result = eitherDecode rawJSON :: Either String FullWeather
in case result of
Left problem -> Nothing
Right ok -> Just ok
字符串FullWeather
而不是FullWeather
extractValues :: BSL.ByteString -> Either String FullWeather
extractValues = eitherDecode
我可以发现三个问题:
(>>=)
和(>>)
应用程序的语法糖。不要对不返回一元值的函数使用do符号(如IO a
,[a]
,可能是a
…)。在do符号之外,正确的语法是let。。。在…
我建议遵循Haskell教程,如,它将教您正确使用do符号
return
的类型为return::Monad m=>a->ma
返回
不是关键字FullWeather
,它也不能返回字符串。这就是或可能的用途。
你可以做的另一件事是抛出一个错误
extractValues :: BSL.ByteString -> FullWeather
extractValues rawJSON =
let result = eitherDecode rawJSON :: Either String FullWeather
in case result of
Left problem -> error problem
Right ok -> ok
可能是FullWeather
而不是FullWeather
extractValues :: BSL.ByteString -> Maybe FullWeather
extractValues rawJSON =
let result = eitherDecode rawJSON :: Either String FullWeather
in case result of
Left problem -> Nothing
Right ok -> Just ok
字符串FullWeather
而不是FullWeather
extractValues :: BSL.ByteString -> Either String FullWeather
extractValues = eitherDecode