制作 PersistBackend 的自定义实例
我有一个以下形式的 monad 转换器堆栈:
newtype T m a = T { unT :: StateT State (SqlPersist m) a }
deriving (Monad, MonadState State)
并且想要使用持久 insert
和 lookup
调用,因此我需要创建一个 PersistBackend
实例对于T
。然而,幻像类型将特定后端编码为 Key 返回类型 - 这会导致一些额外的麻烦。为了解决幻像类型问题,我的实例具有以下形式:
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 ...
我是否盲目地忽略了一种更简单的方法?除了手动提升调用函数之外的另一种方式:insertT = T。举起 。插入
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有两个不同的建议:
MonadState
的做法。我会选择(1)。如果很多人遇到这个问题,我想我们可以有一个专门的包来提供所有功能的提升版本。
I have two different recommendations:
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.