3
我使用ReactiveMongo用MongoDB的一個遊戲框架應用程序,我有以下代碼:創作期貨與理解力
def categories(id: String): Future[Vector[Category]] = {...}
....
val categoriesFuture = categories(id)
for {
categories: Vector[Category] <- categoriesFuture
categoryIdsWithoutPerson: Vector[BSONObjectID] <- findCategoryIdsWithoutPerson(categories.map(_.id), personId) //Returns Future[Vector[BSONObjectID]]
categoriesWithoutPerson: Vector[Category] <- categories.filter(category => categoryIdsWithoutPerson.contains(category.id)) //Play cites the error here
} yield categoryIdsWithoutPerson
爲了解釋這個代碼,我取包裹的Categories
一個Vector
在Future
,因爲這是ReactiveMongo如何滾動。在for
的理解中,我使用Vector
來從數據庫中獲取一個id列表。最後,我使用一個filter
調用來僅保留那些可以在該id列表中找到id的類別。
這一切似乎相當簡單。問題是,遊戲給了我下面的編譯錯誤的for
理解的最後一行:
pattern type is incompatible with expected type;
found : Vector[com.myapp.Category]
required: com.myapp.Category
我不知道爲什麼所需的類型是Category
一個實例。
我可以使用一些洞察力來了解我在做什麼錯誤和/或是否有更簡單或更習慣的方式來完成此操作。
你可以張貼的 '類別' 方法的簽名?以及在哪一行檢測到錯誤? – ale64bit 2014-11-23 21:47:31
按要求澄清。謝謝。 – Vidya 2014-11-23 21:59:15
只是好奇,爲什麼你產生categoryIdsWithoutPerson爲結果?什麼是理解的最後一行? – ale64bit 2014-11-23 22:02:28