I had these lines in my code
val x = database.withSession { implicit session =>
StaticQuery.queryNA[Long](s"select id from .....")
}
val y = database.withSession { implicit session =>
StaticQuery.queryNA[Long](s"select id1 from .....")
}
val z = database.withSession { implicit session =>
StaticQuery.queryNA[(Long, Long)](s"select id1, id2 from .....")
}
In order to minimize code duplication I changed my code to
def genericExec[T](query: String) : List[T] = {
database.withSession { implicit session =>
StaticQuery.queryNA[T](query).list
}
}
for {
x <- genericExec[Long](query1)
y <- genericExec[Long](query2)
z <- genericExec[(Long, Long)](query3)
} {...}
Of course I get an error now that
Error:(16, 32) not enough arguments for method queryNA: (implicit rconv: scala.slick.jdbc.GetResult[T])scala.slick.jdbc.StaticQuery[Unit,T].
Unspecified value parameter rconv.
StaticQuery.queryNA[T](query).list
Since the type T is totally generic its impossible for me to provide all types of conversions. So how can I keep my generic implementation and just provide the conversions which my type T is actually using in my code (aka Long, and (Long, Long)
Because queryNA
has a GetResult
constraint on T
, your function has to have the same constraint. You need to tell your generic method that it is only valid for types T
that have a GetResult
defined, so:
def genericExec[T : GetResult](query: String) : List[T] = ...
or equivalently:
def genericExec[T](query: String)(implicit rconv: GetResult[T]) : List[T] = ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.