summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Kossakowski <grzegorz.kossakowski@gmail.com>2014-02-25 13:50:19 +0100
committerGrzegorz Kossakowski <grzegorz.kossakowski@gmail.com>2014-02-25 13:50:19 +0100
commit1ea43ffb2286d63e133839c1ea0b09449b0ac168 (patch)
tree2d003de0ec0e2687eaef214d65e46922b9e86264
parent7c709e122f7471353749525cff15602783fb348c (diff)
parent12dc4a28da8d3631734603c896d2ee1ba21b31d6 (diff)
downloadscala-1ea43ffb2286d63e133839c1ea0b09449b0ac168.tar.gz
scala-1ea43ffb2286d63e133839c1ea0b09449b0ac168.tar.bz2
scala-1ea43ffb2286d63e133839c1ea0b09449b0ac168.zip
Merge pull request #3566 from adriaanm/t6455
SI-6455 no longer rewrite .withFilter to .filter
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Typers.scala2
-rw-r--r--src/library/scala/util/Try.scala29
-rw-r--r--test/files/neg/t6455.check4
-rw-r--r--test/files/neg/t6455.flags1
-rw-r--r--test/files/neg/t6455.scala6
-rw-r--r--test/files/scalacheck/quasiquotes/TypecheckedProps.scala2
-rw-r--r--test/junit/scala/util/TryTest.scala35
7 files changed, 77 insertions, 2 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
index 8a3ceb3aca..1004777f23 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
@@ -4715,7 +4715,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
UnstableTreeError(qualTyped)
)
val tree1 = name match {
- case nme.withFilter => tryWithFilterAndFilter(tree, qualStableOrError)
+ case nme.withFilter if !settings.future => tryWithFilterAndFilter(tree, qualStableOrError)
case _ => typedSelect(tree, qualStableOrError, name)
}
def sym = tree1.symbol
diff --git a/src/library/scala/util/Try.scala b/src/library/scala/util/Try.scala
index 89db57a55e..b0cf122f2a 100644
--- a/src/library/scala/util/Try.scala
+++ b/src/library/scala/util/Try.scala
@@ -111,6 +111,35 @@ sealed abstract class Try[+T] {
*/
def filter(p: T => Boolean): Try[T]
+ /** Creates a non-strict filter, which eventually converts this to a `Failure`
+ * if the predicate is not satisfied.
+ *
+ * Note: unlike filter, withFilter does not create a new Try.
+ * Instead, it restricts the domain of subsequent
+ * `map`, `flatMap`, `foreach`, and `withFilter` operations.
+ *
+ * As Try is a one-element collection, this may be a bit overkill,
+ * but it's consistent with withFilter on Option and the other collections.
+ *
+ * @param p the predicate used to test elements.
+ * @return an object of class `WithFilter`, which supports
+ * `map`, `flatMap`, `foreach`, and `withFilter` operations.
+ * All these operations apply to those elements of this Try
+ * which satisfy the predicate `p`.
+ */
+ @inline final def withFilter(p: T => Boolean): WithFilter = new WithFilter(p)
+
+ /** We need a whole WithFilter class to honor the "doesn't create a new
+ * collection" contract even though it seems unlikely to matter much in a
+ * collection with max size 1.
+ */
+ class WithFilter(p: T => Boolean) {
+ def map[U](f: T => U): Try[U] = Try.this filter p map f
+ def flatMap[U](f: T => Try[U]): Try[U] = Try.this filter p flatMap f
+ def foreach[U](f: T => U): Unit = Try.this filter p foreach f
+ def withFilter(q: T => Boolean): WithFilter = new WithFilter(x => p(x) && q(x))
+ }
+
/**
* Applies the given function `f` if this is a `Failure`, otherwise returns this if this is a `Success`.
* This is like `flatMap` for the exception.
diff --git a/test/files/neg/t6455.check b/test/files/neg/t6455.check
new file mode 100644
index 0000000000..8f2aad0b9e
--- /dev/null
+++ b/test/files/neg/t6455.check
@@ -0,0 +1,4 @@
+t6455.scala:5: error: value withFilter is not a member of object O
+ O.withFilter(f => true)
+ ^
+one error found
diff --git a/test/files/neg/t6455.flags b/test/files/neg/t6455.flags
new file mode 100644
index 0000000000..112fc720a0
--- /dev/null
+++ b/test/files/neg/t6455.flags
@@ -0,0 +1 @@
+-Xfuture \ No newline at end of file
diff --git a/test/files/neg/t6455.scala b/test/files/neg/t6455.scala
new file mode 100644
index 0000000000..ebbb37f1cd
--- /dev/null
+++ b/test/files/neg/t6455.scala
@@ -0,0 +1,6 @@
+object O { def filter(p: Int => Boolean): O.type = this }
+
+class Test {
+ // should not compile because we no longer rewrite withFilter => filter under -Xfuture
+ O.withFilter(f => true)
+} \ No newline at end of file
diff --git a/test/files/scalacheck/quasiquotes/TypecheckedProps.scala b/test/files/scalacheck/quasiquotes/TypecheckedProps.scala
index 432c0959c9..7c4cb0306e 100644
--- a/test/files/scalacheck/quasiquotes/TypecheckedProps.scala
+++ b/test/files/scalacheck/quasiquotes/TypecheckedProps.scala
@@ -44,7 +44,7 @@ object TypecheckedProps extends QuasiquoteProperties("typechecked") {
val enums = fq"foo <- new Foo" :: fq"if foo != null" :: Nil
val body = q"foo"
val q"$_; for(..$enums1) yield $body1" = typecheck(q"""
- class Foo { def map(f: Any => Any) = this; def filter(cond: Any => Boolean) = this }
+ class Foo { def map(f: Any => Any) = this; def withFilter(cond: Any => Boolean) = this }
for(..$enums) yield $body
""")
assert(enums1 ≈ enums)
diff --git a/test/junit/scala/util/TryTest.scala b/test/junit/scala/util/TryTest.scala
new file mode 100644
index 0000000000..03604a8065
--- /dev/null
+++ b/test/junit/scala/util/TryTest.scala
@@ -0,0 +1,35 @@
+package scala.util
+
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+import org.junit.Test
+import org.junit.Assert._
+
+/* Test Try's withFilter method, which was added along with the -Xfuture fix for SI-6455 */
+@RunWith(classOf[JUnit4])
+class TryTest {
+ @Test
+ def withFilterFail(): Unit = {
+ val fail = for (x <- util.Try(1) if x > 1) yield x
+ assert(fail.isFailure)
+ }
+
+ @Test
+ def withFilterSuccess(): Unit = {
+ val success1 = for (x <- util.Try(1) if x >= 1) yield x
+ assertEquals(success1, util.Success(1))
+ }
+
+ @Test
+ def withFilterFlatMap(): Unit = {
+ val successFlatMap = for (x <- util.Try(1) if x >= 1; y <- util.Try(2) if x < y) yield x
+ assertEquals(successFlatMap, util.Success(1))
+ }
+
+ @Test
+ def withFilterForeach(): Unit = {
+ var ok = false
+ for (x <- util.Try(1) if x == 1) ok = x == 1
+ assert(ok)
+ }
+} \ No newline at end of file