haskell - 在同一功能中混合 Esqueleto 和 Persistent
问题描述
我可能会做一些非常愚蠢的事情,但我想在同一个函数中将一些 Esqueleto 与常规持久查询混合。
我有功能:
handleFactionConstruction :: (BaseBackend backend ~ SqlBackend,
PersistStoreWrite backend, PersistQueryRead backend,
PersistUniqueRead backend, MonadIO m) =>
Time -> Entity Faction -> ReaderT backend m ()
handleFactionConstruction date faction = do
planets <- selectList [ PlanetOwnerId ==. Just (entityKey faction)] []
queues <- mapM loadPlanetConstructionQueue $ map entityKey planets
return ()
并且 loadPlanetConstructionQueue 具有签名(这个执行连接,因此我想在这里使用 Esqueleto):
loadPlanetConstructionQueue :: (MonadIO m, BackendCompatible SqlBackend backend,
PersistQueryRead backend, PersistUniqueRead backend) =>
Key Planet -> ReaderT backend m (Maybe (Entity Planet), [Entity BuildingConstruction
这不起作用,我收到以下错误:
Could not deduce (BackendCompatible SqlBackend backend)
arising from a use of ‘loadPlanetConstructionQueue’
from the context: (BaseBackend backend ~ SqlBackend,
PersistStoreWrite backend, PersistQueryRead backend,
PersistUniqueRead backend, MonadIO m)
bound by the type signature for:
handleFactionConstruction :: forall backend (m :: * -> *).
(BaseBackend backend ~ SqlBackend,
PersistStoreWrite backend,
PersistQueryRead backend,
PersistUniqueRead backend, MonadIO m) =>
Time -> Entity Faction -> ReaderT backend m ()
我认为这与“BackendCompatible SqlBackend backend”和“BaseBackend backend ~ SqlBackend”之间的区别有关。
有没有办法让这个工作?在这种情况下,我可以使用 Esqueleto 编写 selectList 部分,但更进一步,将需要使用替换,Esqueleto 不支持(我认为)。
我对 Haskell、Persistent 或 Esqueleto 了解不多,所以我在这里有点迷茫。
解决方案
您可以添加BackendCompatible SqlBackend backend
到 , 的约束列表handleFactionConstruction
以获得:
handleFactionConstruction :: (BaseBackend backend ~ SqlBackend,
BackendCompatible SqlBackend backend
PersistStoreWrite backend, PersistQueryRead backend,
PersistUniqueRead backend, MonadIO m) =>
Time -> Entity Faction -> ReaderT backend m ()
更一般地说,Could not deduce
错误意味着您的类型签名比它允许的功能之一更通用。有三种方法可以处理这个问题:
- 添加约束,使类型签名更具体(如上)
- 使您正在调用的函数 (
loadPlanetConstructionQueue
) 更通用 - 如果以上都不可能,请以其他方式重写代码