Scala错误:类型参数不符合类类型参数边界

Dan*_*iel 6 scala

请参阅以下函数定义:

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 
      }

}
Run Code Online (Sandbox Code Playgroud)

编译器给我以下错误:

/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]={
Run Code Online (Sandbox Code Playgroud)

以下是对的定义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]{
....
}
Run Code Online (Sandbox Code Playgroud)

知道我哪里错了吗?

更新:以下内容不起作用:

def filterEntity(attribute: DiscreteAttribute[T <: AnyRef], value: String ):Entity[T]={ 
Run Code Online (Sandbox Code Playgroud)

这是错误:

 /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]={
Run Code Online (Sandbox Code Playgroud)

Update2:以下是它的使用方法:

   val filteredConstituent= EdisonDataModel.constituents.filterEntity(EdisonDataModel.Eview,"Token")
Run Code Online (Sandbox Code Playgroud)

哪里

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

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

更新3:以下函数定义存在相同的错误:

  def filterEntity(attribute: DiscreteAttribute[T], value: String ):Unit={
      // empty 
  }
Run Code Online (Sandbox Code Playgroud)

emi*_*ogc 5

您需要为影响filterEntity方法的T类型定义一个限制。

例如class Something[T <: AnyRef],以使其与DiscreteAttribute上的限制匹配

在您的情况下,您需要声明Entity为:class Entity[T <: AnyRef]