# Scala's Dynamic Proxy

One of the coolest features since Scala 2.10.3 are the Scala dynamic types. Let's look at what they are!

A dynamic type is a type with which we can dynamically add fields / methods to an existing type. This is better explained with some examples

Assume that we have a scala class as defined below:

1  class MyClass {
2    def myMethod(str: String) = println(str)
3  }
4  ...
5  ...
6  val myClass1 = new MyClass().myMethod("printme") // Fine!
7  val myClass2 = new MyClass().notMyMethod("beep") // Yes you know it!


For situations like above, scala dynamic types come to the rescue. They are a mechanism by which we can intercept calls to a non-existing field or a method in a class

Let's now modify our MyClass and try to get rind of the compile error when invoking the notMyMethod!

 1  import scala.language.dynamics
2
3  object MyClass extends Dynamic {
4
5    private var myMap = mutable.Map[String, Any]()
6
7    def myMethod(str: String) = println(str)
8
9    def selectDynamic(args: String) = println("selectDynamic " + args)
10
11    def applyDynamic(methodName: String)(args: Any*) = println("applyDynamic " + args)
12
13    def applyDynamicNamed(methodName: String)(args: (String, Any)*) = println("applyDynamicNamed " + args)
14
15    def updateDynamic(name: String)(value: Any) = { myMap(name) = value } // mutating sounds scary!!!
16  }


Let's go through each one of those xxxDynamic methods that we added in the example above:

1. applyDynamic - Dynamically creates a method and the arguments as though that method was part of the declared type
2. applyDynamicNamed - Similar to applyDynamic, with the benefit that we can use a named argument
3. selectDynamic - Dynamically invoke a field as though that field was part of the given type (think of a getter)
4. updateDynamic - Dynamically update a field as though that field was part of the given type - (think of a setter)

I wonder why there is the updateDynamic, I would rather refrain from using it for obvious reasons that we all know (avoid mutability damn it!)

Try the following examples and figure out which xxxDynamic method is invoked in each case

1MyClass.showMyAge("my age is", 34) - ???
2MyClass.FUCK!! - ???
3MyClass.printUser(userName = "Joe", age = "34")  - ???
4MyClass


If you have got all the above examples correctly, you have understood the basics of Scala's dynamic types!