summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-09-01 21:05:11 +0000
committerPaul Phillips <paulp@improving.org>2009-09-01 21:05:11 +0000
commit8ed736aab8dbc8422c70e7d2a6e05bac0a006471 (patch)
tree26fa83d613f1df4cb5aa8633c9c13a958756e2c5 /src
parent337ec4560fa019f0c1e341105b7cbd9e1ef40639 (diff)
downloadscala-8ed736aab8dbc8422c70e7d2a6e05bac0a006471.tar.gz
scala-8ed736aab8dbc8422c70e7d2a6e05bac0a006471.tar.bz2
scala-8ed736aab8dbc8422c70e7d2a6e05bac0a006471.zip
Since I began using scala I have sought an appe...
Since I began using scala I have sought an appealing null coalescing operator. Only now in this dark hour is it upon us! I should find a better place for it, and the rest of my utility functions for dealing sensibly with null.
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/Function.scala29
1 files changed, 28 insertions, 1 deletions
diff --git a/src/library/scala/Function.scala b/src/library/scala/Function.scala
index 95d3aba859..9cdaf90349 100644
--- a/src/library/scala/Function.scala
+++ b/src/library/scala/Function.scala
@@ -11,12 +11,39 @@
package scala
+import annotation.experimental
+
/** A module defining utility methods for higher-order functional programming.
*
* @author Martin Odersky
* @version 1.0, 29/11/2006
*/
-object Function {
+object Function
+{
+ /** Given a starting value, the returned object can be repeatedly
+ * applied with Function1s and then the result retrieved with apply().
+ * At each iteration the argument is checked for null before function
+ * application; if it is ever null, the result will be null.
+ *
+ * <pre>
+ * case class Bop(next: Bop)
+ * val x = Bop(Bop(Bop(null)))
+ * ?:(x)(_.next)() // returns Bop(Bop(null))
+ * ?:(x)(_.next)(_.next)() // returns Bop(null)
+ * ?:(x)(_.next)(_.next)(_.next)() // returns null
+ * ?:(x)(_.next)(_.next)(_.next)(_.next)() // still returns null!
+ * </pre>
+ *
+ * @param x The starting value
+ * @return The ?: object, containing apply methods T => U and () => T
+ */
+ @experimental
+ case class ?:[T](x: T) {
+ def apply(): T = x
+ def apply[U >: Null](f: T => U): ?:[U] =
+ if (x == null) ?:[U](null)
+ else ?:[U](f(x))
+ }
/** Given a sequence of functions <code>f<sub>1</sub></code>, ...,
* <code>f<sub>n</sub></code>, return the function <code>f<sub>1</sub>