-
-
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
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
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