Search code examples
scalagenericsslicktraits

Slick Scala method inferred generic type


So I wanted to implement a trait to have a common execute function to run slick's query.

As you can see from the code below, I have one trait that has a type parameter on the class and the other define the type parameter on the method.

When I compile, the trait with method generic type compiles(without giving any type argument) but the other one does not.

Why??? I tried to give the class type arguments UserTable or User (my slick table def and projected case class) but none of them works. The error just says "expects DBIO[UserTable] but actual MySQLDriver.StreamingDriverAction"

Any help really appreciated.

Thanks a lot!!!

class DAO @Inject()(val configProvider: DatabaseConfigProvider) extends 
    ManagementAppDatabase {
    private val users = TableQuery[UserTable]

  def findUserByEmail(email: String): Future[Option[User]] = {
    execute(users.filter(_.email === email).result.headOption)
  }
}

trait ManagementAppDatabase {
  val configProvider: DatabaseConfigProvider
 def execute[T](dBIO:DBIO[T]): Future[T] = configProvider.get[JdbcProfile].db.run(dBIO)
}

trait ManagementAppDatabase[T] {
  val configProvider: DatabaseConfigProvider
 def execute (dBIO:DBIO[T]):Future[T]=configProvider.get[JdbcProfile].db.run(dBIO)
}

Solution

  • If you extend e.g. ManagementAppDatabase[User], then you can only call execute on DBIO[User]. But users.filter(_.email === email).result.headOption is DBIO[Option[User]]. That's it.