Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala forward or delegate methods to encapsulated object

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.

like image 227
Kamil Lelonek Avatar asked Nov 20 '14 09:11

Kamil Lelonek


1 Answers

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")
like image 113
0__ Avatar answered Oct 29 '22 14:10

0__