首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Haskell Servant空路由组合器

Haskell Servant空路由组合器
EN

Stack Overflow用户
提问于 2017-03-25 07:52:49
回答 1查看 110关注 0票数 2

我正在写一些CRUD助手,为了好玩和利润,我发现自己需要一条空的或无路可走的路线。:>mempty,如果您愿意的话。

这是我想写的:

代码语言:javascript
复制
type Index model =
  Reassoc (QueryParams model :> Get '[JSON] (Collection model))

type family Reassoc xs where
  Reassoc ((x :> y) :> z) = Reassoc (x :> Reassoc (y :> z))
  Reassoc (x :> y) = x :> y

type family QueryParams model

type instance QueryParams User =
  MakeQueryParams '[ '("organizationId", Int) ]

当然,这一切都建立在这个人身上:

代码语言:javascript
复制
type family MakeQueryParams xs where
  MakeQueryParams ( '(sym, ty) ': xs ) 
    = QueryParam sym ty :> MakeQueryParams xs
  MakeQueryParams '[] 
    = ... :(

是否有空的路由组合器?

到目前为止,我已经通过在这些家庭中使用next参数解决了这个问题,但对于Servant来说,这还不是很常见。

代码语言:javascript
复制
type family MakeQueryParams xs next where
    MakeQueryParams '[] next =
        next
    MakeQueryParams ('(sym, ty) ': xs) next =
        QueryParam sym ty :> MakeQueryParams xs next

type Index model = QueryParams model (Get '[JSON] (Collection model))

type family QueryParams model next

type instance QueryParams User next =
    MakeQueryParams '[ '("organizationId", Int) ] next
EN

回答 1

Stack Overflow用户

发布于 2019-02-27 01:40:37

如果你真的坚持要写

代码语言:javascript
复制
type API = QueryParams '[ '("id", Int) ] :> Get '[JSON] Bool

你将你的类似foldr的想法/解决方案(这是完全好的)与一个新的组合器结合在一起:

代码语言:javascript
复制
data QueryParams (ps :: [(Symbol, *)])

instance HasServer api ctx
  => HasServer (QueryParams '[] :> api) ctx where
    type ServerT (QueryParams '[] api) m = ServerT api m

    route = ...

instance HasServer (QueryParam sym ty :> MakeQueryParams ('(sym, ty) ': ps) api) ctx
  => HasServer (QueryParams ('(sym, ty) ': ps) api) ctx where
    type ServerT (QueryParams ('(sym, ty) ': ps) api) m = ...

    route = ...

为nil和cons情况编写单独的实例将使实例的实现更直接。

可以认为我们必须引入新的combinator,否则我们无法在正确的位置插入类型族。有点像我们有时需要编写newtype来编写不同的实例。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43010941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档