2015-08-19 7 views
7

Sto tentando di modificare un tratto che ho usato per le query del database Scala Slick. Ecco due metodi che ho finora:Sottotipo per un elemento tabella in una Scala Slick Query

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] 

voglio modificare queste due firme del metodo per consentire sottotipi di T. Un'istanza di questo è se ho una definizione di tratto per un record, ma ho bisogno di un'implementazione concreta di quel tratto da usare effettivamente per slick. Ho provato a fare qualcosa di simile:

/** 
* 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] 

tuttavia ottengo un errore di compilazione come segue:

[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]            ^

e non sono sicuro di cosa fare per ottenere il risultato che voglio.

risposta

4

Idealmente, si dovrebbe fare variante query con T. Ma dal momento che è fuori del vostro controllo, si potrebbe fare così (dovrebbe funzionare):

protected def find[U <: T](t: U): Query[Table[_], U, Seq] 

ma sento che si tratta di un problema più grande qui. Perché hai bisogno di una tale astrazione? Qual è il tuo design di classe?