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

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