Cristian Boariu
Cristian Boariu

Reputation: 9621

Scala generics issues

I have a companion object like this:

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
    }
  }

of case class Address.

Because I have many such objects (each defining a table) I want to move findAll method in a trait making it generic and I tried to do it like this:

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
    }
  }
}

As you notice, entities is basically my companion object.

The issue now is that I cannot rewrite my object definition like this:

object Addresses extends Table[Address]("address") with DbGenericOperations[Address, Addresses]{

because it says that Addresses is not a type...

I am quite new to scala and I'm wondering: is there any way to solve out this issue?

UPDATE: I've done it like this:

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
        }
      }
    }

and declare companion object like this: object Addresses extends Table[Address]("address") with DbGenericOperations[Address]

but I don't like the use of this method:

val results = Addresses.findAll(limit, offset, Addresses)

Please let me know if there is a better solution...

Upvotes: 2

Views: 123

Answers (1)

Jakozaur
Jakozaur

Reputation: 1977

Why not create a class/trait RichTable[T](...) extends Table[T](...) which will add this method?

Upvotes: 1

Related Questions