Skip to content

Instantly share code, notes, and snippets.

@bartschuller
Created August 7, 2011 14:09
Show Gist options
  • Save bartschuller/1130405 to your computer and use it in GitHub Desktop.
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
// 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