【发布时间】:2015-07-07 07:04:32
【问题描述】:
我正在使用运算符<|>:
import qualified Data.ByteString.Lazy as B
import Network.HTTP.Conduit (simpleHttp)
import Data.Aeson
import Data.Maybe
data FooBar = FooBar {
name :: !Text,
surname :: !Text
} deriving (Show,Generic)
instance FromJSON FooBar
instance ToJSON FooBar
getFeed :: String -> String -> IO (FooBar)
getFeed foo bar = decode <$> (B.readFile foo <|> simpleHttp bar)
但是当我尝试编译它时,我得到:
No instance for (Alternative IO) arising from a use of ‘<|>’
In the second argument of ‘(<$>)’, namely
‘(B.readFile foo <|> simpleHttp bar)’
In the expression:
decode <$> (B.readFile foo <|> simpleHttp bar)
In an equation for ‘getFeed’:
getFeed env id
= decode <$> (B.readFile foo <|> simpleHttp bar)
这个错误对我来说有点模糊。知道如何解决吗? (顺便说一句,从这个回复中得到一些见解:Confused by the meaning of the 'Alternative' type class and its relationship to other type classes)
【问题讨论】:
-
你想用
<|>达到什么目的? -
如果第一个“readFile”由于某种原因失败(如文件未找到)移动到第二个,基本上是“如果存在则...否则...”的通用替代方案。尽管它们看起来具有相同的输出(字节字符串),但我认为它们也需要以某种方式“继承”替代 =>。
标签: haskell io-monad alternative-functor