Making Custom Instances of PersistBackend

僤鯓⒐⒋嵵緔 提交于 2019-12-12 13:25:09

问题


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


回答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.



来源:https://stackoverflow.com/questions/8992040/making-custom-instances-of-persistbackend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!