简体   繁体   English

Scala错误:类型参数不符合类类型参数范围

[英]Scala error: type arguments do not conform to class type parameter bounds

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

And here is the definition of the DiscreteAttribute : 这是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 am going wrong? 知道我要去哪里错了吗?

Update: The following does not work either: 更新:以下内容也不起作用:

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: here is how it's been used: Update2:使用方法如下:

   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 holds for the following function definition: 更新3:相同的错误适用于以下函数定义:

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

You need to define a restriction for the T type that is affecting the filterEntity method. 您需要为影响filterEntity方法的T类型定义一个限制。

eg class Something[T <: AnyRef] so that it matches the restriction on DiscreteAttribute 例如, class Something[T <: AnyRef] ,使其与DiscreteAttribute上的限制匹配

In your case you want to have the declaration of Entity as: class Entity[T <: AnyRef] . 在您的情况下,您想将Entity声明为: class Entity[T <: AnyRef]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scala:类型参数不符合类类型参数范围 - Scala: type arguments do not conform to class type parameter bounds scala错误:类型参数不符合类类型参数边界[X&gt;:A,Y&gt;:A] - scala error: type arguments do not conform to class type parameter bounds [X >: A,Y >: A] 理解Scala中的“类型参数不符合类型参数边界”错误 - Understanding “type arguments do not conform to type parameter bounds” errors in Scala Scala 类型 arguments 不符合类型参数界限 - Scala type arguments do not conform to type parameter bounds 理解类型 arguments do not conform to class type parameter bounds error when using Higher kinded type parameter - Understanding type arguments do not conform to class type parameter bounds error when using Higher kinded type parameter Scala:类型参数不符合特征Subtractable的类型参数范围 - Scala: type arguments do not conform to trait Subtractable's type parameter bounds 理解Scala中的“推断类型参数不符合类型参数边界”错误 - Understanding “inferred type arguments do not conform to type parameter bounds” errors in Scala Scala 类型 arguments 不符合方法 eval 的类型参数界限 - Scala type arguments do not conform to method eval's type parameter bounds 类型参数不符合特征栏的类型参数范围 - type arguments do not conform to trait Bar's type parameter bounds 类型参数不符合特征Subtractable的类型参数边界 - type arguments do not conform to trait Subtractable's type parameter bounds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM