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

scala – 特征继承的依赖性

发布时间:2020-12-16 18:10:22 所属栏目:安全 来源:网络整理
导读:在 Scala中,我如何将容器特征(如Traversable [Content])添加到另一个扩展容器的容器(因此其内容的可见性有限? 例如,下面的代码试图为需要Traversable的容器定义特征WithIter(当然,我实际上在Container中有其他东西). import scala.collection._trait Contai
在 Scala中,我如何将容器特征(如Traversable [Content])添加到另一个扩展容器的容器(因此其内容的可见性有限?

例如,下面的代码试图为需要Traversable的容器定义特征WithIter(当然,我实际上在Container中有其他东西).

import scala.collection._

trait Container {
  type Value
}

trait WithIter extends Container with immutable.Traversable[Container#Value]

class Instance extends WithIter {
  type Value = Int
  def foreach[U](f : (Value) => (U)) : Unit = {}
}

编译器(scalac 2.8.0.Beta1-RC8)发现错误:

error: class Instance needs to be abstract,since method foreach in trait GenericTraversableTemplate of type [U](f: (Container#Value) => U)Unit is not defined

有什么简单的方法吗?

解决方法

class Instance extends WithIter {
  type Value = Int
  def foreach[U](f : (Container#Value) => (U)) : Unit = {}
}

如果你在谈到内部类时没有指定OuterClass#,那么这个. (即,特定于实例)将被假定.

(编辑:李大同)

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

    推荐文章
      热点阅读