import scala.tools.nsc.reporters._ import scala.tools.nsc.Settings import reflect.runtime.Mirror.ToolBox object Test extends App { val code = scala.reflect.Code.lift{ case class Person(name: String, age: Int) /** An AddressBook takes a variable number of arguments * which are accessed as a Sequence */ class AddressBook(a: Person*) { private val people: List[Person] = a.toList /** Serialize to XHTML. Scala supports XML literals * which may contain Scala expressions between braces, * which are replaced by their evaluation */ def toXHTML = { for (p <- people) yield }
Name Age
{ p.name } { p.age.toString() }
; } /** We introduce CSS using raw strings (between triple * quotes). Raw strings may contain newlines and special * characters (like \) are not interpreted. */ val header = { "My Address Book" } ; val people = new AddressBook( Person("Tom", 20), Person("Bob", 22), Person("James", 19)); val page = { header } { people.toXHTML } ; println(page) }; val reporter = new ConsoleReporter(new Settings) val toolbox = new ToolBox(reporter) toolbox.runExpr(code.tree) }