为了减少将 Haxl 与关系数据库一起使用所需的样板文件的目标,我试图通过 Persistent 将原始 SQL 请求的结果打包为存在量化类型。但是类型检查器不允许它:
data SomeRawSql where
SomeRawSql :: forall b. RawSql b => [b] -> SomeRawSql
packedVal = let res = runDB $ rawSql "SELECT * FROM ..." [toPersistValue (pack "ABC")]
in fmap SomeRawSql res
这会导致 fmap 出现类型错误:Ambiguous type variable ‘b0’ arising from a use of ‘SomeRawSql’ prevents the constraint ‘(RawSql b0)’ from being solved.
持久化 rawSql 的类型是:
rawSql :: (RawSql a, MonadIO m)
=> Text -- ^ SQL statement, possibly with placeholders.
-> [PersistValue] -- ^ Values to fill the placeholders.
-> ReaderT SqlBackend m [a]
runDB
是一个辅助函数,它连接到数据库并返回IO [a]
. 根据 rawSql 的定义,我希望能满足 RawSql 约束。我不明白为什么会出现这个错误。