我正在尝试修改我用于Scala Slick数据库查询的特征。到目前为止,我有两种方法:
protected def findByPrimaryKey(id: PrimaryKeyType): Query[Table[_], T, Seq]
/**
* return the row that corresponds with this record
* @param t - the row to find
* @return query - the sql query to find this record
*/
protected def find(t: T): Query[Table[_], T, Seq]
我想修改这两个方法签名以允许T
的子类型。一个例子是如果我有一个记录的特征定义,但需要具体实现该特征实际用于光滑。我尝试过这样的事情:
/**
* return all rows that have a certain primary key
* @param id
* @return Query object corresponding to the selected rows
*/
protected def findByPrimaryKey(id: PrimaryKeyType): Query[Table[_], _ <: T, Seq]
/**
* return the row that corresponds with this record
* @param t - the row to find
* @return query - the sql query to find this record
*/
protected def find(t: T): Query[Table[_], _ <: T, Seq]
但是我收到如下编译错误:
[error] found : slick.driver.PostgresDriver.simple.Query[slick.driver.PostgresDriver.simple.Table[_],_$8,Seq] where type _$8 <: T
[error] (which expands to) scala.slick.lifted.Query[slick.driver.PostgresDriver.Table[_],_$8,Seq]
[error] required: slick.driver.PostgresDriver.simple.Query[slick.driver.PostgresDriver.simple.Table[_],T,Seq]
[error] (which expands to) scala.slick.lifted.Query[slick.driver.PostgresDriver.Table[_],T,Seq]
[error] Note: _$8 <: T, but class Query is invariant in type U.
[error] You may wish to investigate a wildcard type such as `_ <: T`. (SLS 3.2.10)
[error] val query: Query[Table[_], T, Seq] = find(t)
[error] ^
[error] /home/chris/dev/suredbits-core/src/main/scala/com/suredbits/core/db/CRUDActor.scala:58: type mismatch;
[error] found : slick.driver.PostgresDriver.simple.Query[slick.driver.PostgresDriver.simple.Table[_],_$8,Seq] where type _$8 <: T
[error] (which expands to) scala.slick.lifted.Query[slick.driver.PostgresDriver.Table[_],_$8,Seq]
[error] required: slick.driver.PostgresDriver.simple.Query[slick.driver.PostgresDriver.simple.Table[_],T,Seq]
[error] (which expands to) scala.slick.lifted.Query[slick.driver.PostgresDriver.Table[_],T,Seq]
[error] Note: _$8 <: T, but class Query is invariant in type U.
[error] You may wish to investigate a wildcard type such as `_ <: T`. (SLS 3.2.10)
[error] val query: Query[Table[_], T, Seq] = find(t)
[error] ^
我不确定如何做到我想要的结果。
答案 0 :(得分:4)
理想情况下,您应该使用T创建Query变量。但由于这不受您的控制,您可以这样做(它应该可以):
{{1}}
但我觉得你在这里处理的是一个更大的问题。你为什么需要这样的抽象?你的班级设计是什么?