期待一种类型但实际上得到了一个Maybe a0

时间:2015-05-17 10:18:00

标签: json haskell types aeson

我有一个使用Aeson从文件中的json对象获取数据的函数。数据和功能都定义如下。

data Data = Data { date :: String
                   , temperature :: Int
                    } deriving (Show, Read, Generic)

data Temperatures = Temperatures { temperatures :: [Data]
                                 } deriving (Show, Generic)

mooStuff :: IO Temperatures
mooStuff  = decode <$> getFile
    where
        getFile = B.readFile "Hello.txt"

该函数使用的文件包含:

Data {date = "2015-02-28T20:16:12+00:00", temperature = 0}
Data {date = "2015-01-01T21:46:55+00:00", temperature = 2}
Data {date = "2015-04-08T21:46:53+00:00", temperature = 3}
Data {date = "2015-04-09T21:46:01+00:00", temperature = 4}
Data {date = "2015-04-10T21:46:40+00:00", temperature = 5}
Data {date = "2015-04-11T21:46:36+00:00", temperature = 6}
Data {date = "2015-04-12T20:36:25+00:00", temperature = 7}

但是我收到了类型错误:

test.hs:37:13:
    Couldn't match type ‘Maybe a0’ with ‘Temperatures’
    Expected type: B.ByteString -> Temperatures
      Actual type: B.ByteString -> Maybe a0
    In the first argument of ‘(<$>)’, namely ‘decode’
    In the expression: decode <$> getFile

我现在已经盯着这几个小时,但没有用。关于我出错的地方的任何想法都将非常感谢!

1 个答案:

答案 0 :(得分:11)

错误的原因是decode并不总是成功。它的类型是

decode :: Data a => ByteString -> Maybe a

相应地,

(decode <$>) :: Data a => IO ByteString -> IO (Maybe a)
                          -- desired type, IO Temperatures, won't unify

类型错误消息为您提供了抽象Maybe a0作为实际类型,因为它检测到MaybeTemperatures之间的冲突,然后才能选择适当的实例Data a类型中的decode

您的代码需要考虑文件传递其内容的可能性,但他们没有成功decodeTemperatures

这样的东西
do mt <- decode <$> getFile
   case mt of
     Nothing -> -- handle failure here
     Just t  -> return t

或者如果您对decode未能触发IO“用户错误”感到高兴,只需

do Just t <- decode <$> getFile
   return t

任何方式,都有一个代码无法解决的失败模式,而这就是类型错误的表现。