Created
August 7, 2011 14:09
-
-
Save bartschuller/1130405 to your computer and use it in GitHub Desktop.
Example of Scala's Dynamic trait as implemented in Scala 2.9.0 with scalac -Xexperimental
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
// Compile this with scala 2.9.x and an extra option: | |
// scalac -Xexperimental dyn.scala | |
/** | |
* Example to show off the Dynamic trait. | |
* | |
* Create an instance, call methods on it (the methods return this | |
* so you can chain calls), then call result. | |
**/ | |
class ListBuilder extends Dynamic { | |
private var res = List[String]() | |
def applyDynamic(method: String)(args: Any*) = { | |
val argString = if (args.length>0) "(" + args.mkString(" ") + ")" else "" | |
res = method + argString :: res | |
this | |
} | |
def result = res.reverse | |
} | |
object ListBuilder { | |
def main(args: Array[String]) { | |
val lb = new ListBuilder | |
lb.any.method("with", 100, "parameters").you.like | |
println(lb.result) | |
// List(any, method(with 100 parameters), you, like) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment