我在尝试编写返回Future [Map [Int,Long]]的方法时遇到问题.
我正在进行一些迭代,回到未来[未来[..]].所以我尝试了flatMap身份.
请参阅下面的我现在收到的代码和错误消息.我不知道此刻发生了什么.
def aggregate(permissions: Vector[Permission]): Map[Int, Long]
def calculate(roleProfile: RoleProfile): Future[Map[Int, Long]] = {
val roleIds = roleProfile.roles.map(r => r.id)
db.run(permissionDao.getByRoles(roleIds.toList)).map {
permissions =>
aggregate(permissions)
}
}
def generate(roleGroupId: Int): Future[Map[Int, Long]] = {
for {
roleGroup <- roleGroupService.getById(roleGroupId)
roles <- roleGroupService.getRolesByGroupId(roleGroupId)
} yield {
calculate(RoleProfile(roleGroup.get.id, roles.toSet)) //flatMap identity
}
}
Run Code Online (Sandbox Code Playgroud)
我收到方法'计算'的错误消息:
type mismatch;
[error] found : scala.concurrent.Future[Map[Int,Long]]
[error] required: Map[Int,Long]
[error] calculate(RoleProfile(roleGroup.get.id, roles.toSet)) //flatMap identity
Run Code Online (Sandbox Code Playgroud)
现在,如果删除'flatMap identity'的注释,我会收到此错误:
type mismatch;
[error] found : Map[Int,Long] => Map[Int,Long]
[error] required: Map[Int,Long] => scala.concurrent.Future[?]
[error] calculate(RoleProfile(roleGroup.get.id, roles.toSet)) flatMap identity
Run Code Online (Sandbox Code Playgroud)
我很困惑,我怎么能得到它来返回Future [Map [Int,Long]].
更重要的是,这里发生的事情我似乎并不理解.如果你非常感激,请把事情搞清楚.
因为calculate,就像你的getByIdetc调用一样Future,你应该能够简单地将它添加到for-comprehension的主要部分,例如:
def generate(roleGroupId: Int): Future[Map[Int, Long]] = {
for {
roleGroup <- roleGroupService.getById(roleGroupId)
roles <- roleGroupService.getRolesByGroupId(roleGroupId)
profile <- calculate(RoleProfile(roleGroup.get.id, roles.toSet))
} yield { profile }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
194 次 |
| 最近记录: |