创建PersistBackend的自定义实例 [英] Making Custom Instances of PersistBackend

查看:99
本文介绍了创建PersistBackend的自定义实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  newtype T ma = T {unT :: StateT State(SqlPersist) m)a)
导出(Monad,MonadState State)

并且想要使用persistent insert lookup 调用,所以我需要做一个 PersistBackend 实例为 T 。但是,幻像类型将特定的后端编码到 Key 返回类型中 - 这会导致一些额外的麻烦。为了解决幻像类型问题,我的实例具有如下形式:

pre $实例(Monad m,MonadIO m,MonadBaseControl IO m)= > PersistBackend T m其中
insert = T。电梯 。 liftM(Key。unKey)。插入
...以及大约十几种这样的方法...

我是否会盲目地俯瞰更简单的方法?除了通过手动提升来调用函数之外的其他方法: insertT = T。电梯 。插入

解决方案

我有两个不同的建议:


  1. 翻转所有内容,并将SqlPersist放在您的StateT中,而不是反过来。 使用提升版本的功能创建模块,类似到 MonadState

我会选择(1)。如果有很多人遇到这种情况,我想我们可以有一个专门的软件包来提供所有功能的提升版本。


I have a monad transformer stack of the form:

newtype T m a = T { unT :: StateT State (SqlPersist m) a }
   deriving (Monad, MonadState State)

And want to use the persistent insert and lookup calls, so I need to make a PersistBackend instance for T. However, a phantom type encodes the specific backend into the Key return type - this causes some extra headaches. To solve the phantom type issue, my instance has the form:

instance (Monad m, MonadIO m, MonadBaseControl IO m) => PersistBackend T m where
   insert = T . lift . liftM (Key . unKey) . insert
   ... and about a dozen such methods ...

Am I blindly overlooking an easier way? A way other than calling the function by manually lifting: insertT = T . lift . insert

解决方案

I have two different recommendations:

  1. Flip things around, and put the SqlPersist around your StateT instead of vice-versa.
  2. Create a module with lifted versions of the functions, similar to what MonadState does.

I'd go for (1). If a lot of people are running into this, I suppose we could have a specialized package providing the lifted versions of all the functions.

这篇关于创建PersistBackend的自定义实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆