Scala forward or delegate methods to encapsulated object

981 views Asked by At

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.

1

There are 1 answers

0
0__ On BEST ANSWER

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")