Is it possible to call an overridden method from self type?

后端 未结 3 636
独厮守ぢ
独厮守ぢ 2021-01-07 23:32

Consider this:

 class Foo { def foo = \"foo\" }
 trait Bar { self: Foo =>
    override def foo = \"bar\"
 }

I was pleasantly surprised

3条回答
  •  青春惊慌失措
    2021-01-08 00:39

    You make your trait extend Foo instead of using the self type:

    class Foo {def foo = "foo"}
    trait Bar extends Foo {
      override def foo = super.foo + "bar"
    }
    new Foo with Bar foo // barfoo
    

    See also this answer.

提交回复
热议问题