slick reusable InsertAndUpdate trait
鉴于 DRY,我试图避免在 Slick 表定义中重复插入和更新逻辑。我试过这个:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | trait Entity { def id: Option[Int] } case class BankRekening(id: Option[Int], nummer: String, omschrijving: String) extends Entity{ } object BankRekeningen extends Table[BankRekening]("bankrekening") with InsertAndUpdate[BankRekening] { def id = column[Int]("id", O.PrimaryKey, O.AutoInc) def nummer = column[String]("nummer") def omschrijving = column[String]("omschrijving") def * = id.? ~ nummer ~ omschrijving <> (BankRekening, BankRekening.unapply _) def idx_nummer = index("idx_nummer", nummer, unique = true) } trait InsertAndUpdate[T <: Entity] { this: Table[T] => def id: scala.slick.lifted.Column[Int] def insert(obj: T): T = { obj.copy(id = Some(this.returning(this.id) insert obj)) } } |
现在编译器在最后一条语句中抱怨 \\'obj\\',说:找不到 scala.slick.lifted.TypeMapper[T]
类型的证据参数的隐式值
有什么想法吗?
简单的答案:您尝试在没有复制方法的 T 上调用复制。
长答案:不幸的是,Scalac,Scala 编译器让我们 Slick(无意中)劫持错误消息。您应该看到错误
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | object simple{ implicit def intColumnType: TypedType[Int] = null implicit def valueToConstColumn[T : TypedType](v: T) = ConstColumn(v) } import simple._ trait TypedType[T] case class ConstColumn[T](v: T){ def bar = 5 } trait InsertAndUpdate[T] { def insert(obj: T): T = { 5.copy() // <- unexpected, but is valid and compiles fine 5.bar // compiles fine obj.bar // error: value bar is not a member of type parameter T // error: could not find implicit value for evidence parameter of type TypedType[T] obj.copy() // error: could not find implicit value for evidence parameter of type TypedType[T] } } |
如您所见,消息以某种方式被