Scala error: type arguments do not comply with class class parameter restrictions

See the following function definition:

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 } } 

The compiler gives me the following error:

 /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 type parameter bounds [T <: AnyRef] [error] def filterEntity(attribute: DiscreteAttribute[T], value: String ):Entity[T]={ 

And here is the definition of 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]{ .... } 

Any idea where I'm wrong?

Update: The following also does not work:

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

Here is the error:

  /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: this is how it was used:

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

Where

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

and

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

Update 3: The same error occurs for the following function definition:

  def filterEntity(attribute: DiscreteAttribute[T], value: String ):Unit={ // empty } 
+5
source share
1 answer

You need to define a constraint for type T that affects the filterEntity method.

eg. class Something[T <: AnyRef] so that it matches the DiscreteAttribute limit

In your case, you want to have an Entity declaration as: class Entity[T <: AnyRef] .

+4
source

All Articles