Created
March 28, 2012 04:32
-
-
Save clhodapp/2223633 to your computer and use it in GitHub Desktop.
Dynamic example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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