如何为类约束值指定类型?

时间:2015-02-13 06:51:14

标签: haskell yesod typeclass persistent monad-transformers

我尝试在以下persistent调用的结果上使用类型签名。我无法弄清楚如何为参数化和类约束的内容定义签名,例如下面变换器内的m

saveMyStuff :: MonadIO m => Int -> SqlPersistT m Stuff
saveMyStuff somethingId = do
  -- 1. this works with ScopedTypeVariables
  (_ :: [Entity MyEntity]) <- rawSql "SELECT ?? FROM my_entities WHERE id = ? FOR UPDATE" 
                                     [toPersistValue somethingId]
  -- 2. this doesn't :(
  _ <- rawSql "SELECT ?? FROM my_entities WHERE id = ? FOR UPDATE" 
              [toPersistValue somethingId]
              :: SqlPersistT m [Entity MyEntity]

我如何使用后一种方法?是2.完全可以解决没有扩展?我将该签名更改为什么?

2 个答案:

答案 0 :(得分:3)

在类型签名中添加forall

saveMyStuff :: forall m. MonadIO m => Int -> SqlPersistT m Stuff

使用ScopedTypeVariables时,变量仅在forall明确引入时确定范围。

答案 1 :(得分:0)

原来我没有意识到你需要在函数内部重复约束。这很有效:

_ <- rawSql "SELECT ?? FROM my_entities WHERE id = ? FOR UPDATE" 
          [toPersistValue somethingId]
          :: MonadIO m => SqlPersistT m [Entity MyEntity]