Scala forward or delegate methods to encapsulated object
Asked Answered
U

1

6

Is there any possibility to implicitly forward some of class methods to encapsulated object?

case class Entity(id: Int, name: String,) {
  private lazy val lastScan = new LastScan

  def getLastScanDate    = lastScan.getLastScanDate
  def updateLastScanDate = lastScan.updateLastScanDate
}

I want to avoid creating def updateLastScanDate = lastScan.updateLastScanDate just to forward methods to wrapped object.

Unionist answered 20/11, 2014 at 9:19 Comment(0)
M
2

In the plain language this is not possible. There used to be a compiler plugin by Kevin Wright to achieve this automatic delegation.

He seems to be working on an Autorproxy "Rebooted" version now that is macro based, making it straight forward to include in your project. I'm pasting here an example from its test sources:

trait Bippy {
  def bippy(i : Int): String
}

object SimpleBippy extends Bippy {
  def bippy(i: Int) = i.toString
}

@delegating class RawParamWrapper(@proxy pivot: Bippy)
val wrapper = new RawParamWrapper(SimpleBippy)
assert(wrapper.bippy(42) == "42")
Mccool answered 20/11, 2014 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.