Scala Slick Query中表元素的子类型

Chr*_*art 7 scala slick

我正在尝试修改我用于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]
Run Code Online (Sandbox Code Playgroud)

我想修改这两个方法签名以允许子类型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]
Run Code Online (Sandbox Code Playgroud)

但是我得到一个编译错误如下:

[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]                                                  ^
Run Code Online (Sandbox Code Playgroud)

而且我不确定如何做到我想要的结果.

Rok*_*alj 4

理想情况下,您应该使用 T 创建 Query 变体。但由于这是您无法控制的,您可以这样做(它应该可以工作):

protected def find[U <: T](t: U): Query[Table[_], U, Seq]
Run Code Online (Sandbox Code Playgroud)

但我感觉你正在处理一个更大的问题。为什么需要这样的抽象?你们的班级设计是什么?