如何在通过特征覆盖方法时调用super方法

时间:2013-10-08 00:19:25

标签: scala polymorphism mixins traits

似乎可以在具有如下特征的类上更改方法的实现:

trait Abstract { self: Result =>
    override def userRepr = "abstract"
}

abstract class Result {
    def userRepr: String = "wtv"
}

case class ValDefResult(name: String) extends Result {
    override def userRepr = name
}

val a = new ValDefResult("asd") with Abstract
a.userRepr

现场代码可在此处找到:http://www.scalakata.com/52534e2fe4b0b1a1c4daa436

但是现在我想调用函数的上一个或超级实现,如下所示:

trait Abstract { self: Result =>
    override def userRepr = "abstract" + self.userRepr
}

trait Abstract { self: Result =>
    override def userRepr = "abstract" + super.userRepr
}

然而,这些替代方案都没有编译。知道如何实现这一目标吗?

3 个答案:

答案 0 :(得分:17)

这是我正在寻找的答案。感谢Shadowlands使用Scala的abstract override功能指向我正确的方向。

trait Abstract extends Result {
    abstract override def userRepr = "abstract " + super.userRepr
}

abstract class Result {
    def userRepr: String = "wtv"
}

case class ValDefResult(name: String) extends Result {
    override def userRepr = name
}

val a = new ValDefResult("asd") with Abstract
a.userRepr

现场代码可在此处找到:http://www.scalakata.com/52536cc2e4b0b1a1c4daa4a4

对于令人困惑的示例代码感到抱歉,我正在编写一个处理Scala AST的库,但没有足够的灵感来改变名称。

答案 1 :(得分:12)

我不知道您是否能够进行以下更改,但是您可以通过引入额外的特征(我将其称为Repr)并使用{{}来实现您想要的效果。 1 {} abstract override特征:

Abstract

您的示例用法现在提供:

trait Repr {
    def userRepr: String
}

abstract class Result extends Repr {
    def userRepr: String = "wtv"
}

case class ValDefResult(name: String) extends Result {
    override def userRepr = name
}

trait Abstract extends Repr { self: Result =>
    abstract override def userRepr = "abstract-" + super.userRepr // 'super.' works now
}

答案 2 :(得分:10)

abstract override是机制,也就是可堆叠的特征。值得补充的是线性化计数,因为这决定了super的含义。

这个问题是the canonical Q&A on self-type vs extension的一个很好的附录。

继承与自我类型不明确的地方:

scala> trait Bar { def f: String = "bar" }
defined trait Bar

scala> trait Foo { _: Bar => override def f = "foo" }
defined trait Foo

scala> new Foo with Bar { }
<console>:44: error: <$anon: Foo with Bar> inherits conflicting members:
  method f in trait Foo of type => String  and
  method f in trait Bar of type => String
(Note: this can be resolved by declaring an override in <$anon: Foo with Bar>.)
              new Foo with Bar { }
                  ^

显然,你可以选择:

scala> new Foo with Bar { override def f = super.f }
res5: Foo with Bar = $anon$1@57a68215

scala> .f
res6: String = bar

scala> new Foo with Bar { override def f = super[Foo].f }
res7: Foo with Bar = $anon$1@17c40621

scala> .f
res8: String = foo

scala> new Bar with Foo {}
res9: Bar with Foo = $anon$1@374d9299

scala> .f
res10: String = foo