kevinwright edited this page Sep 13, 2010 · 3 revisions

The autoproxy-plugin is a simple idea that’s surprisingly difficult to implement correctly in the scala compiler.
Essentially, it works with a single annotation and generates some synthetic methods in a class. This is easier to explain with an example:

class Foo {
  val prop0 = "prop0"
  def method0 = "method0"
  def method1(arg: String) = "method1(" + arg + ")"
}

class Bar {
  @proxy val myFoo : Foo = new RichFoo
  def barmethod0 = "barmethod0"
}

The idea behind the @proxy annotation is that it creates delegates for
all members of the annotated member, thusly:

class Bar {
  val myFoo : Foo = new RichFoo
  def barmethod0 = "barmethod0"

  // These are synthesized
  def prop0 = myFoo.prop0
  def method0 = myFoo.ethod0
  def method1(arg: String) = myFoo.method1(arg)
}

All methods defined on Foo can then be called on an instance of Bar:

object Main {
  def main(args : Array[String]) {
    val bar = new Bar
    println(bar.prop1)
    println(bar.method0)
    println(bar.barmethod0)
  }
}

Despite being simple, this technique allows for some powerful Use Cases while avoiding a lot of boilerplate code.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.