Skip to content

Instantly share code, notes, and snippets.

@jlandahl
Last active August 29, 2015 13:59
Show Gist options
  • Save jlandahl/10794935 to your computer and use it in GitHub Desktop.
Save jlandahl/10794935 to your computer and use it in GitHub Desktop.
trait EntityBase {
def id: String
}
trait Repository {
type Entity <: EntityBase
def get(id: String): Option[Entity]
}
trait SlickRepository extends Repository {
val profile: scala.slick.driver.JdbcProfile
def database: profile.backend.Database
import profile.simple._
abstract class BaseTable(tag: Tag, tableName: String) extends Table[Entity](tag, tableName) {
def id: Column[String]
}
val table: TableQuery[BaseTable] // needs to be covariant
override def get(id: String): Option[Entity] = database.withSession { implicit session =>
table.filter(_.id === id).list.headOption
}
}
case class User(id: String, name: String, email: String) extends EntityBase
trait UserRepository extends SlickRepository {
type Entity = User
import profile.simple._
class UserTable(tag: Tag) extends BaseTable(tag, "user") {
def id = column[String]("userid", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def email = column[String]("email")
def * = (id, name, email) <> (User.tupled, User.unapply)
}
override val table = TableQuery[UserTable] // doesn't compile -- incompatible type
}
@jlandahl
Copy link
Author

Error:(46, 16) overriding value table in trait SlickRepository of type UserRepository.this.profile.simple.TableQuery[UserRepository.this.BaseTable];
value table has incompatible type
override val table = TableQuery[UserTable] // doesn't compile -- incompatible type
^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment