0
object BugFixProject {
def main (args: Array[String]) {
val repoWithEntities = Seq(
(ARepo, Seq(A("", ""), A("", ""))),
(BRepo, Seq(B("", ""), B("", "")))
)
printall(repoWithEntities)
}
def printall[Entity](pairs: Seq[(BaseRepo[Entity], Seq[Entity])]) : Seq[String] = {
pairs.map { t =>
val repo = t._1
val entities = t._2
entities.map(repo.toString(_))
}.flatten
}
}
case class A(foo: String, bar: String){}
case class B(foo: String, bar: String){}
trait BaseRepo[X] {
def toString(x: X) : String = x.toString
}
object BRepo extends BaseRepo[B] {}
object ARepo extends BaseRepo[A] {}
printall应该像主要方法中所述的那样使用。但后来我在编译过程中出现以下错误:斯卡拉动态类型
Error:(12, 9) no type parameters for method printall: (pairs: Seq[(BaseRepo[Entity], Seq[Entity])])Seq[String] exist so that it can be applied to arguments (Seq[(BaseRepo[_ >: B with A <: Product with Serializable], Seq[Product with Serializable])])
--- because ---
argument expression's type is not compatible with formal parameter type;
found : Seq[(BaseRepo[_ >: B with A <: Product with Serializable], Seq[Product with Serializable])]
required: Seq[(BaseRepo[?Entity], Seq[?Entity])]
printall(repoWithEntities)
^
而且
Error:(12, 18) type mismatch;
found : Seq[(BaseRepo[_ >: B with A <: Product with Serializable], Seq[Product with Serializable])]
required: Seq[(BaseRepo[Entity], Seq[Entity])]
printall(repoWithEntities)
^
我不明白的错误消息。是否有可能在
def printall[Entity]
实体只能是1特定类型?
如果您知道进入方法的内容,则不需要“实体”类型参数。你在这里试图做什么并不是很清楚,你有没有尝试重写代码以使其更清晰? –
姆赫。我不知道如何澄清... 我想用'repoWithEntities'调用'printall',但不知道方法签名的外观如何... 'printall'应该可以调用一个值类型为'Seq [(BaseRepo [Entity],Seq [Entity])]',其中“Entity”仅表示一个Tuple必须包含一个具有相同类型序列的BaseRepo。如示例中所示,repoWithEntities可以具有包含不同类型的不同回收的元组。 –
在对'printall'的任何单个调用中,是的,'Entity'必须是特定类型的。 –