制作 PersistBackend 的自定义实例

发布于 2024-12-29 06:04:12 字数 601 浏览 3 评论 0原文

我有一个以下形式的 monad 转换器堆栈:

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

并且想要使用持久 insertlookup 调用,因此我需要创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街九尾狐 2025-01-05 06:04:12

我有两个不同的建议:

  1. 翻转过来,将 SqlPersist 放在 StateT 周围,而不是反之亦然。
  2. 使用函数的提升版本创建一个模块,类似于 MonadState 的做法。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文