2015-09-17 12 views
5

vedere la seguente definizione di funzione:Scala di errore: argomenti di tipo non sono conformi al tipo di classe limiti dei parametri

class Entity[T](
       val pi : T => String, 
       val si : T => Map[Symbol,String], 
       val tag : ClassTag[T], 
       val address: T=>AnyRef 
) { 
     // some other definitions here ... 
     def filterEntity(attribute: DiscreteAttribute[T], value: String):Unit={ 
     // nothing 
     } 

} 

Il compilatore mi dà il seguente errore:

/Users/i-danielk/ideaProjects/saul/src/main/scala/edu/illinois/cs/cogcomp/lfs/data_model/entity/Entity.scala:67: type arguments [T] do not conform to class DiscreteAttribute's type parameter bounds [T <: AnyRef] 
[error] def filterEntity(attribute: DiscreteAttribute[T], value: String):Entity[T]={ 

Ed ecco la definizione di il DiscreteAttribute:

case class DiscreteAttribute[T <: AnyRef](
           val name : String, 
           val mapping: T => String, 
           val range : Option[List[String]] 
           )(implicit val tag : ClassTag[T]) extends TypedAttribute[T,String]{ 
.... 
} 

Hai idea di dove sto andando male?

Update: Il seguente non funziona neanche:

def filterEntity(attribute: DiscreteAttribute[T <: AnyRef], value: String):Entity[T]={ 

Ecco l'errore:

/Users/i-danielk/ideaProjects/saul/src/main/scala/edu/illinois/cs/cogcomp/lfs/data_model/entity/Entity.scala:67: ']' expected but '<:' found. 
[error] def filterEntity(attribute: DiscreteAttribute[T <: AnyRef], value: String):Entity[T]={ 

Update2: ecco come è stato utilizzato:

val filteredConstituent= EdisonDataModel.constituents.filterEntity(EdisonDataModel.Eview,"Token") 

dove

object EdisonDataModel extends DataModel { 
    val Eview = discreteAttributeOf[Constituent]('CviewName){ 
    x=>x.getViewName 
} 

e

def discreteAttributeOf[T <: AnyRef](name : Symbol)(f : T => String)(implicit tag : ClassTag[T]) : DiscreteAttribute[T] = { 
    new DiscreteAttribute[T](name.toString,f,None) 
    } 

Update 3: Lo stesso errore vale per la seguente definizione di funzione:

def filterEntity(attribute: DiscreteAttribute[T], value: String):Unit={ 
     // empty 
    } 
+2

Da dove proviene il 'T' in' filterEntity'? –

+0

L'esempio di codice qui è incompleto. Pubblica l'ambiente di definizione di filterEntity – Daenyth

risposta

4

È necessario definire una restrizione per il tipo T che sta interessando il metodo filterEntity.

ad es. class Something[T <: AnyRef] in modo che corrisponda alla restrizione su DiscreteAttribute

Nel tuo caso si desidera avere la dichiarazione di Entity come: class Entity[T <: AnyRef].

+0

Vedi anche questo non funziona: 'def filterEntity (attributo: DiscreteAttribute [T <: AnyRef], value: String): Entity [T] = {' – Daniel

+0

Devi cambiarlo non nell'uso ma la definizione. Vedi i commenti sulla domanda dicendo che il tuo esempio è incompleto. – emilianogc

+0

Ho completato la definizione della funzione (il primo codice). Qualche idea? – Daniel

Problemi correlati