1
votes

I want to reuse code in my app,

for example, have functions like this

  def getA1(idUser: Option[Long]) = {
    val q = A1Q.filter(_.idUser === idUser.get)
    Await.result(db.run(q.result), Duration.Inf)
  }

  def getA2[T](idUser: Option[Long]) = {
    val q = A2Q.filter(_.idUser === idUser.get)
    Await.result(db.run(q.result), Duration.Inf)
  }

  def getAn(idUser: Option[Long]) = {
    val q = AnQ.filter(_.idUser === idUser.get)
    Await.result(db.run(q.result), Duration.Inf)
  }

A1Q has a type TableQuery

and 'q' has type Query[TA1Q, TA1Q#TableElementType, scala.Seq] tried to use generic for solve current issue, like this

 def execQ[T](q: Query[T, T, scala.Seq]) = Await.result(db.run(q.result), Duration.Inf)

but types mis much,

How can I generic Query[TA1Q, TA1Q#TableElementType, scala.Seq] ? in the correct way and prepare some separated function like

  def execQ[T](q: Query[T, T, scala.Seq]) = Await.result(db.run(q.result), Duration.Inf)

BR!

1

1 Answers

0
votes

You need two type parameters:

First C which is your Entity and therefore the return type of your query. And second T which is your entities table and therefore extends Table[C].

def execQ[C,T <: Table[C]](q: Query[T, C, scala.Seq]) = Await.result(db.run(q.result), Duration.Inf)