简体   繁体   中英

In Scala how can I advise my own methods?

I want to do this:

trait Renderable {
    def render: String
}

trait Parens extends Renderable {
    abstract override def render = "(" + super.render + ")"
}

object Foo extends Renderable with Parens {
    def render = "Hello"
}

But this does not work because the linearization order puts Parens after Foo (Foo always comes, of course) so Parens can't advise Foo.render.

I end up doing this:

trait FooRender {
    def render = "Hello"
}

object Foo extends FooRender with Parens {
}

But sometimes I really don't want to do that because it breaks things up. As far as I can tell, linearization order is the only thing getting in the way, but I don't know a way to change that. What might make this cleaner?

How about separating the presentation logic ( render ) and the actual contents ( value )?

trait Renderable {
  def value : String
  def render = value // default presentation logic, simple rendering.
}

trait Parens extends Renderable {
  override def render :String = "(" + value + ")" // parens rendering.
}

object Foo extends Parens {
  def value = "Hello"
}

println(Foo.render) // prints '(Hello)'

Edit

Found a way you can do pretty much what you wanted, check it out:

trait Renderable {
  def render: String
}

trait Parens extends Renderable {
  abstract override def render = "(" + super.render + ")"
}

class Foo extends Renderable {
  def render = "Hello"
}

val foo = new Foo with Parens
println(foo.render)

You can't use an object since you need to implement the trait at creation time, but if you can control the instance creation then this might work :).

I'm pretty sure it's not possible to do it otherwise (someone please correct me if I'm wrong)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM