【发布时间】:2014-06-22 15:25:58
【问题描述】:
我一直在尝试在路线中使用Maybe a。到目前为止我试过了
/u/#Maybe UserId
/u/#(Maybe UserId)
/u/#Maybe-UserId
和
/u/#MaybeUserId
在哪里
type MaybeUserId = Maybe UserId
但没有多大成功。
奇怪的是,#Maybe-UserId 与使用 Maybe UserId 的处理程序编译得很好,但是,即使使用下面的新 PathPiece 实例,它也无法找到匹配项。
instance (PathPiece a) => PathPiece (Maybe a) where
fromPathPiece s = case s of
"Nothing" -> Nothing
_ -> Just $ fromPathPiece s
toPathPiece m = case m of
Just s -> toPathPiece s
_ -> "Nothing"
创建Maybe 路由并且不必为我想使用的每个Maybe a 声明一个类型和实例,我缺少什么?
编辑:由于某种原因,当使用Ǹothing 以外的任何东西时,实例工作正常。
edit2: "Nothing" -> Nothing 实际上表示 PathPiece 解析失败,这不是预期的结果。 "Nothing" -> Just Nothing 做正确的事。
【问题讨论】:
标签: haskell routes yesod maybe