-
-
Save pedrofurla/10801461 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.simple.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 | |
abstract class 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) | |
} | |
val table:TableQuery[BaseTable] = TableQuery[UserTable] | |
} |
info] Compiling 1 Scala source to /Users/pedrofurla/dev/projects/slick/slick-perf/target/scala-2.10/classes...
[error] /Users/pedrofurla/dev/projects/slick/slick-perf/src/main/scala/slickperf/irc.scala:45: type mismatch;
[error] found : scala.slick.lifted.TableQuery[UserRepository.this.UserTable]
[error] required: UserRepository.this.profile.simple.TableQuery[UserRepository.this.BaseTable]
[error](which expands to) scala.slick.lifted.TableQuery[UserRepository.this.BaseTable]
[error] Note: UserRepository.this.UserTable <: UserRepository.this.BaseTable, but class TableQuery is invariant in type E.
[error] You may wish to investigate a wildcard type such as _ <: UserRepository.this.BaseTable
. (SLS 3.2.10)
[error] val table:TableQuery[BaseTable] = TableQuery[UserTable]
[error] ^
import scalaz.syntax.traverse._
import scalaz.std.AllInstances._ // IDEA says it's not used, but it's a lie
val rs: List[List[Throwable \/ Report]] = ls map { _.sequence } // IDEA says it's an error, it's not!
val rs2 : List[Throwable \/ List[Report]] = rs.transpose map { _.sequenceU }
Have you found a way to get around this? I'm struggling with a way to come up with a base repository like this, running into those same "value table has incompatible type" errors.
Error:(45, 7) overriding value table in trait SlickRepository of type UserRepository.this.profile.simple.TableQuery[UserRepository.this.BaseTable];
value table has incompatible type
val table = TableQuery[UserTable]
^