Skip to content

Instantly share code, notes, and snippets.

@stephanos
Forked from bartschuller/dyn.scala
Created October 20, 2012 09:57
Show Gist options
  • Save stephanos/3922829 to your computer and use it in GitHub Desktop.
Save stephanos/3922829 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
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
}
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