Skip to content

Instantly share code, notes, and snippets.

@clhodapp
Created March 28, 2012 04:32
Show Gist options
  • Save clhodapp/2223633 to your computer and use it in GitHub Desktop.
Save clhodapp/2223633 to your computer and use it in GitHub Desktop.
Dynamic example
object Dynamic {
class DynamicBox1(val value: Any) extends Dynamic {
override def applyDyamic[@specialized A](name: String, arg1: A): Dynamic = {
val method = value.getClass.getMethod(name, arg1.getClass)
boxDynamic(method.invoke(value, arg1.asInstanceOf[AnyRef]))
}
override def as[T] = value.asInstanceOf[T]
}
def boxDynamic(a: Any): Dynamic = {
if (a.isInstanceOf[Dynamic]) a.asInstanceOf[Dynamic]
else new DynamicBox1(a)
}
}
trait Dynamic {
def applyDyamic[@specialized A](name: String, arg1: A): Dynamic = {
val myClass = this.getClass
val method = myClass.getMethod(name, arg1.getClass)
Dynamic.boxDynamic(method.invoke(this, arg1.asInstanceOf[AnyRef]))
}
def as[T] = this.asInstanceOf[T]
}
class Foo extends Dynamic {
def bar(x: Int) = x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment