diff options
author | Aleksandar Prokopec <aleksandar.prokopec@gmail.com> | 2011-12-07 14:53:37 +0100 |
---|---|---|
committer | Aleksandar Prokopec <aleksandar.prokopec@gmail.com> | 2011-12-07 14:53:37 +0100 |
commit | 014a13d2be634bb3ab3468d0c071a9a870a9a9b0 (patch) | |
tree | 491ad18ca796a70b34be1d4732b80c371bfd650d /test/pending/run/reify_properties.scala | |
parent | a36175feb5bfce59909fa4f3d9d5df6753b6ee3a (diff) | |
parent | 332fec96e31840878bed41dd7b5314b97d8da7c2 (diff) | |
download | scala-014a13d2be634bb3ab3468d0c071a9a870a9a9b0.tar.gz scala-014a13d2be634bb3ab3468d0c071a9a870a9a9b0.tar.bz2 scala-014a13d2be634bb3ab3468d0c071a9a870a9a9b0.zip |
Merge branch 'master' of https://github.com/scala/scala into execution-context
Diffstat (limited to 'test/pending/run/reify_properties.scala')
-rw-r--r-- | test/pending/run/reify_properties.scala | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/test/pending/run/reify_properties.scala b/test/pending/run/reify_properties.scala new file mode 100644 index 0000000000..2115a96715 --- /dev/null +++ b/test/pending/run/reify_properties.scala @@ -0,0 +1,63 @@ +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{ + /** A mutable property whose getter and setter may be customized. */ + case class Property[T](init: T) { + private var value: T = init + + /** The getter function, defaults to identity. */ + private var setter: T => T = identity[T] + + /** The setter function, defaults to identity. */ + private var getter: T => T = identity[T] + + /** Retrive the value held in this property. */ + def apply(): T = getter(value) + + /** Update the value held in this property, through the setter. */ + def update(newValue: T) = value = setter(newValue) + + /** Change the getter. */ + def get(newGetter: T => T) = { getter = newGetter; this } + + /** Change the setter */ + def set(newSetter: T => T) = { setter = newSetter; this } + } + + class User { + // Create a property with custom getter and setter + val firstname = Property("") + .get { v => v.toUpperCase() } + .set { v => "Mr. " + v } + val lastname = Property("<noname>") + + /** Scala provides syntactic sugar for calling 'apply'. Simply + * adding a list of arguments between parenthesis (in this case, + * an empty list) is translated to a call to 'apply' with those + * arguments. + */ + override def toString() = firstname() + " " + lastname() + } + + val user1 = new User + + // Syntactic sugar for 'update': an assignment is translated to a + // call to method 'update' + user1.firstname() = "Robert" + + val user2 = new User + user2.firstname() = "bob" + user2.lastname() = "KUZ" + + println("user1: " + user1) + println("user2: " + user2) + }; + + val reporter = new ConsoleReporter(new Settings) + val toolbox = new ToolBox(reporter) + val ttree = toolbox.typeCheck(code.tree) + toolbox.runExpr(ttree) +} |