Scala泛型问题
我有一个这样的伴星:Scala泛型问题,scala,Scala,我有一个这样的伴星: object Addresses extends Table[Address]("address"){ //some mapping columns .... //a method I want to made generic def findAll(limit: Option[Int], offset: Option[Int]): Seq[Address] = DBTest.db.withSession { implicit db: Session
object Addresses extends Table[Address]("address"){
//some mapping columns
....
//a method I want to made generic
def findAll(limit: Option[Int], offset: Option[Int]): Seq[Address] = DBTest.db.withSession { implicit db: Session =>
(limit, offset) match {
case (Some(l), Some(o)) => Addresses.map { a => a }.drop(o).take(l).list
case (None, None) => Addresses.map { a => a }.drop(ConfigurationsLoader.DefaultOffset).take(ConfigurationsLoader.DefaultLimit).list
case (Some(l), None) => Addresses.map { a => a }.take(l).list
case (None, Some(o)) => Addresses.map { a => a }.drop(o).list
}
}
trait DbGenericOperations[T, U <: Table[T]]{
val entities: U
/**
* Based on REST conventions, for listing it's good to use limit and offset, like: /entities?limit=25&offset=50.
*/
def findAll(limit: Option[Int], offset: Option[Int]): Seq[T] = DBTest.db.withSession { implicit db: Session =>
(limit, offset) match {
case (Some(l), Some(o)) => entities.map { a => a }.drop(o).take(l).list
case (None, None) => entities.map { a => a }.drop(ConfigurationsLoader.DefaultOffset).take(ConfigurationsLoader.DefaultLimit).list
case (Some(l), None) => entities.map { a => a }.take(l).list
case (None, Some(o)) => entities.map { a => a }.drop(o).list
}
}
}
trait DbGenericOperations[T]{
/**
* Based on REST conventions, for listing it's good to use limit and offset, like: /entities?limit=25&offset=50.
*/
def findAll(limit: Option[Int], offset: Option[Int], entities: Table[T]): Seq[T] = DBTest.db.withSession { implicit db: Session =>
(limit, offset) match {
case (Some(l), Some(o)) => entities.map { a => a }.drop(o).take(l).list
case (None, None) => entities.map { a => a }.drop(ConfigurationsLoader.DefaultOffset).take(ConfigurationsLoader.DefaultLimit).list
case (Some(l), None) => entities.map { a => a }.take(l).list
case (None, Some(o)) => entities.map { a => a }.drop(o).list
}
}
}
案例类别的地址
因为我有很多这样的对象(每个对象定义一个表),所以我想将findAll
方法移动到一个特性中,使其成为通用的,我尝试这样做:
object Addresses extends Table[Address]("address"){
//some mapping columns
....
//a method I want to made generic
def findAll(limit: Option[Int], offset: Option[Int]): Seq[Address] = DBTest.db.withSession { implicit db: Session =>
(limit, offset) match {
case (Some(l), Some(o)) => Addresses.map { a => a }.drop(o).take(l).list
case (None, None) => Addresses.map { a => a }.drop(ConfigurationsLoader.DefaultOffset).take(ConfigurationsLoader.DefaultLimit).list
case (Some(l), None) => Addresses.map { a => a }.take(l).list
case (None, Some(o)) => Addresses.map { a => a }.drop(o).list
}
}
trait DbGenericOperations[T, U <: Table[T]]{
val entities: U
/**
* Based on REST conventions, for listing it's good to use limit and offset, like: /entities?limit=25&offset=50.
*/
def findAll(limit: Option[Int], offset: Option[Int]): Seq[T] = DBTest.db.withSession { implicit db: Session =>
(limit, offset) match {
case (Some(l), Some(o)) => entities.map { a => a }.drop(o).take(l).list
case (None, None) => entities.map { a => a }.drop(ConfigurationsLoader.DefaultOffset).take(ConfigurationsLoader.DefaultLimit).list
case (Some(l), None) => entities.map { a => a }.take(l).list
case (None, Some(o)) => entities.map { a => a }.drop(o).list
}
}
}
trait DbGenericOperations[T]{
/**
* Based on REST conventions, for listing it's good to use limit and offset, like: /entities?limit=25&offset=50.
*/
def findAll(limit: Option[Int], offset: Option[Int], entities: Table[T]): Seq[T] = DBTest.db.withSession { implicit db: Session =>
(limit, offset) match {
case (Some(l), Some(o)) => entities.map { a => a }.drop(o).take(l).list
case (None, None) => entities.map { a => a }.drop(ConfigurationsLoader.DefaultOffset).take(ConfigurationsLoader.DefaultLimit).list
case (Some(l), None) => entities.map { a => a }.take(l).list
case (None, Some(o)) => entities.map { a => a }.drop(o).list
}
}
}
并像这样声明伴随对象:
对象地址使用DbGenericOperations[Address]扩展表[Address](“Address”)
但我不喜欢使用这种方法:
val结果=地址。findAll(限制、偏移、地址)
请告诉我是否有更好的解决方案…为什么不创建一个类/特征RichTable[T](…)extends Table[T](…)来添加此方法?因为当我必须扩展Table[T]时,我还必须指定表名:
extends Table[Address](“Address”)
所以我不再使用泛型案例…不明白。抽象类RichTable[T](名称:String)扩展表[T](名称){…}