- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个称为 Mutatable 的特征,它会生成一个实现类的修改副本。我还想在其之上添加一个称为 CostedMutatable 的特征,它可以跟踪这样做的成本。 applyMutation 方法返回一个 Option,稍后我想在特定突变不适用的情况下返回 None。
仅适用于整数(并通过添加新数字来“改变”它们)的简单版本如下所示:
trait Mutatable[M] {
def applyMutation(mut : M) : Option[this.type]
}
trait CostedMutatable[M] extends Mutatable[M]{
var cost : Int = _
def getCostFor(mut : M): Int
abstract override def applyMutation(mut : M) : Option[this.type] = {
cost += getCostFor(mut)
applyMutation(mut)
}
}
object Example extends App {
case class Mutation(x: Int)
class Test(s: Int) extends Mutatable[Mutation] {
val start = s
override def applyMutation(mut: Mutation): Option[Test]
= Some(new Test(s+mut.x))
}
class CostTest(s: Int) extends Test(s) with CostedMutatable[Mutation] {
override def getCostFor(mut: Mutation): Int = 2
}
val testCost = new CostTest(5).cost
}
问题是,这无法编译。我在编译时收到以下错误:
Error:(23, 18) overriding method applyMutation in trait Mutatable of type (mut: Example.Mutation)Option[Test.this.type];
method applyMutation has incompatible type
override def applyMutation(mut: Mutation): Option[Test] = Some(new Test(s+mut.x))
^
除了编译器错误之外,我还想到了另一个问题:我是否以正确的方式处理这个问题?我应该使用 F 边界类型吗? (我需要每个新的实现类从 applyMutation 返回具体实现类的新副本。)
提前致谢。
最佳答案
this.type
是一种类型,其唯一实例是 this
和 Nothing
。当方法返回 this.type
时,唯一允许的返回值是 this
。在 Test
类中 applyMutation
不会返回 this
,而是返回一个全新的 Test
,它不是一个this.type
的实例。这就是代码不进行类型检查的原因。
我认为你真正想做的是声明 applyMutation 返回与 this
相同类的值。这样做确实需要 F-Bounded 多态性。这是代码的重写版本:
trait CostedMutatable[+A <: CostedMutatable[A, M], M] extends Mutatable[A, M] {
var cost : Int = _
def getCostFor(mut : M): Int
abstract override def applyMutation(mut: M): Option[A] = {
cost += getCostFor(mut)
super.applyMutation(mut)
}
}
object Example extends App {
case class Mutation(x: Int)
class Test(s: Int) extends Mutatable[Test, Mutation] {
val start = s
override def applyMutation(mut: Mutation): Option[Test]
= Some(new Test(s+mut.x))
}
class CostTest(s: Int) extends Test(s) with CostedMutatable[CostTest, Mutation] {
override def getCostFor(mut: Mutation): Int = 2
}
val testCost = new CostTest(5).cost
}
关于Scala Stackable Trait 和 Self Type 不兼容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22746471/
我正在提供安装可堆叠文件系统的目录中任何文件的路径。有没有办法以编程方式检查挂载的文件系统是可堆叠文件系统还是我的底层下层文件系统? 我已经尝试使用 statfs(2),但它总是返回成功,因为它检测到
我有一张 table ,我想使用 stackable.js (http://johnpolacek.github.io/stacktable.js/) 来响应移动设备,我的 table 在数据类型的基
我对这两个类之间的区别感到困惑 Threaded和 Stackable 在 pthreads PHP 扩展。 PHP 手册页没有提到 Stackable,但它存在并且许多关于 pthreads 的文本
我有一个称为 Mutatable 的特征,它会生成一个实现类的修改副本。我还想在其之上添加一个称为 CostedMutatable 的特征,它可以跟踪这样做的成本。 applyMutation 方法返
我正在为我的 Rails 元素使用 Semantic-UI,实际上我正在使用 https://github.com/doabit/semantic-ui-sass ,一切都很好,除非我对响应式网格使用
最近几天我将 PHP 线程 (pthreads) 与 PHP 7.0.1(在配置中使用 –enable-maintainer-zts)一起使用,并且在使用 Thread 或 Worker 时一切正常。
大家好, 我正在为我的网页游戏构建一个拖放库存面板,但我无法使其与可堆叠元素一起使用。我简化了整个 list ,以减少混淆。 首先,让我解释一下我希望它如何工作: 每个 .item 元素都可以放在任何
我是一名优秀的程序员,十分优秀!