加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 18:39:29 所属栏目:安全 来源:网络整理
导读:请参阅以下函数定义: 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 ):Un
请参阅以下函数定义:

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 
      }

}

编译器给我以下错误:

/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]={

这是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]{
....
}

知道我哪里错了吗?

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

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

这是错误:

/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:以下是它的使用方法:

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

哪里

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

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

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

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

解决方法

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

例如class Something [T&lt ;: AnyRef],以便它匹配DiscreteAttribute的限制

在您的情况下,您希望将实体声明为:class Entity [T&lt ;: AnyRef].

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读