2015-08-19 19 views
7

Scala Slick veritabanı sorguları için kullanmış olduğum bir özelliği değiştirmeye çalışıyorum. Bugüne kadar sahip İşte iki yöntem:Scala Slick Query'de bir tablo öğesi için alt öğe

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] 

Ben T alt tipleri için izin vermek, bu iki yöntem imzaları değiştirmek istiyor. Bunun bir örneği, bir kayıt için bir özellik tanımladığım, ancak bu özellik için, gerçekten kaygan kullanmak için somut bir uygulamaya ihtiyaç duymasıdır.

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

ve ben istediğimiz sonucu elde etmek için ne emin değilim şu şekildedir:

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

ancak bir derleme hatası alıyorum: Ben böyle bir şey yaptığını denedim.

cevap

4

İdeali, T. ile Sorgu varyantını yapmalıdır Ama bu sizin kontrolünüz dışında olduğundan bu (o çalışmalıdır) gibi, bunu yapabilirdi:

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

Ama bir ilgileniyor anlamda Burada daha büyük sorun. Neden böyle bir soyutlamaya ihtiyacınız var? Sınıf tasarımın nedir?

İlgili konular