summaryrefslogtreecommitdiff
path: root/test/files/presentation/properties/src/properties.scala
diff options
context:
space:
mode:
authorMicro Dotta <mirco.dotta@gmail.com>2011-08-17 13:32:25 +0000
committerMicro Dotta <mirco.dotta@gmail.com>2011-08-17 13:32:25 +0000
commit6ba1b9f3c974351e826485ca9c41df732c6de15a (patch)
tree25c08330ac9c176353cc98f6a3f6cbd0c541d07d /test/files/presentation/properties/src/properties.scala
parent044099d4f1677425719ea8cad8c946dab8b5c2d9 (diff)
downloadscala-6ba1b9f3c974351e826485ca9c41df732c6de15a.tar.gz
scala-6ba1b9f3c974351e826485ca9c41df732c6de15a.tar.bz2
scala-6ba1b9f3c974351e826485ca9c41df732c6de15a.zip
Major rewrite of the testing infrastructure for...
Major rewrite of the testing infrastructure for the presentation compiler. Added several new tests that will be part of the nightly build. Once the move to SBT is completed I will look into how to extract the test infrastructure (as it should really not be living in the compiler codebase). Review by dragos
Diffstat (limited to 'test/files/presentation/properties/src/properties.scala')
-rw-r--r--test/files/presentation/properties/src/properties.scala54
1 files changed, 54 insertions, 0 deletions
diff --git a/test/files/presentation/properties/src/properties.scala b/test/files/presentation/properties/src/properties.scala
new file mode 100644
index 0000000000..35b6a92221
--- /dev/null
+++ b/test/files/presentation/properties/src/properties.scala
@@ -0,0 +1,54 @@
+/** Illustrate the use of custom 'apply/update' methods. */
+object properties {
+
+ /** 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()
+ }
+
+ def main(args: Array[String]) {
+ 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)
+ }
+} \ No newline at end of file