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 следует использовать как указано в основном методе. Но тогда я получаю следующее сообщение об ошибке во время компиляции:Scala dynamic type
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 конкретного типа?
Вам не нужен параметр типа 'Entity', если вы знаете, что происходит в методе. Не совсем понятно, что вы пытаетесь сделать здесь, попробовали ли вы переписать свой код немного, чтобы сделать его более понятным? –
Mhhm. Я не знаю, как уточнить ... Я хочу, чтобы 'printall' вызывался с' repoWithEntities', но не знаю, как должна выглядеть подпись метода ... 'printall' должен быть вызван со значением типа 'Seq [(BaseRepo [Entity], Seq [Entity])]' с «Entity», просто присутствуя там, чтобы показать, что кортеж должен содержать BaseRepo с последовательностью того же типа. Как показано в примере, repoWithEntities может иметь кортежи, содержащие разные репозиции разных типов. –
В любом одном вызове 'printall', да,' Entity' должен быть в определенном типе. –