我不能使它编译和类型匹配

时间:2016-03-31 04:22:47

标签: haskell

在我的仆人应用程序中,我有这个:

getItems :: Maybe String -> ExceptT ServantErr IO [MyData]
getItems myHdr = 
  case myHdr of
    Nothing -> throwE err403
    Just myHdrVal -> do
      case myHdrVal of
        "aaabbbcc" -> liftIO $ do
          print "gooood!"
          query_ cn "select * from table1"

        _ -> throwE err403 -- I want to add "print"

这很好用。但是,由于类型不匹配,以下内容无法编译:

  _ -> print "not gooood" >> throwE err403

  -- or
  _ -> liftIO $ do 
    print "not gooood"
    throwE err403

  -- or
  _ -> liftIO =<< (print "not gooood" >> throwE err403) -- incorrect, though

如何解决?

1 个答案:

答案 0 :(得分:5)

根据我的评论以及haoformayor的鼓励,这应该可以解决问题:

  _ -> do 
    liftIO $ print "not gooood"
    throwE err403

请注意,您只需抬起print,因为这是#34;工作&#34;在IO monad但不是throwE,因为它显然已经返回正确的类型

当然

_ -> liftIO (print "not gooood") >> throwE err403
如果你愿意,

也应该工作

声明

我没有测试过这个