Skip to content

Commit 32b7564

Browse files
committed
SI-8022 Backwards compatibility for Regex#unapplySeq
The change in ce1bbfe / SI-6406 introduced overloads of `unapplySeq` with wider static and dynmaic result types than the now-deprecated alternative that accepted `Any`. This is subtly source incompatible and the change was noticed in Specs2. This commit uses `List` as the static and runtime type for the new overloads. For consistency, the same is done for the new method added in SI-7737 / 93e9623.
1 parent 073ebbd commit 32b7564

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/library/scala/util/matching/Regex.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ class Regex private[matching](val pattern: Pattern, groupNames: String*) extends
188188
* @param s The string to match
189189
* @return The matches
190190
*/
191-
def unapplySeq(s: CharSequence): Option[Seq[String]] = {
191+
def unapplySeq(s: CharSequence): Option[List[String]] = {
192192
val m = pattern matcher s
193-
if (runMatcher(m)) Some(1 to m.groupCount map m.group)
193+
if (runMatcher(m)) Some((1 to m.groupCount).toList map m.group)
194194
else None
195195
}
196196

@@ -225,10 +225,10 @@ class Regex private[matching](val pattern: Pattern, groupNames: String*) extends
225225
* @param c The Char to match
226226
* @return The match
227227
*/
228-
def unapplySeq(c: Char): Option[Seq[Char]] = {
228+
def unapplySeq(c: Char): Option[List[Char]] = {
229229
val m = pattern matcher c.toString
230230
if (runMatcher(m)) {
231-
if (m.groupCount > 0) Some(m group 1) else Some(Nil)
231+
if (m.groupCount > 0) Some((m group 1).toList) else Some(Nil)
232232
} else None
233233
}
234234

@@ -238,9 +238,9 @@ class Regex private[matching](val pattern: Pattern, groupNames: String*) extends
238238
* Otherwise, this Regex is applied to the previously matched input,
239239
* and the result of that match is used.
240240
*/
241-
def unapplySeq(m: Match): Option[Seq[String]] =
241+
def unapplySeq(m: Match): Option[List[String]] =
242242
if (m.matched == null) None
243-
else if (m.matcher.pattern == this.pattern) Some(1 to m.groupCount map m.group)
243+
else if (m.matcher.pattern == this.pattern) Some((1 to m.groupCount).toList map m.group)
244244
else unapplySeq(m.matched)
245245

246246
/** Tries to match target.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
package scala.util.matching
3+
4+
import org.junit.Assert._
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
import org.junit.runners.JUnit4
8+
9+
@RunWith(classOf[JUnit4])
10+
class RegexTest {
11+
@Test def t8022CharSequence(): Unit = {
12+
val full = """.*: (.)$""".r
13+
val text = " When I use this operator: *"
14+
// Testing 2.10.x compatibility of the return types of unapplySeq
15+
val x :: Nil = full.unapplySeq(text: Any).get
16+
val y :: Nil = full.unapplySeq(text: CharSequence).get
17+
assertEquals("*", x)
18+
assertEquals("*", y)
19+
}
20+
21+
@Test def t8022Match(): Unit = {
22+
val R = """(\d)""".r
23+
val matchh = R.findFirstMatchIn("a1").get
24+
// Testing 2.10.x compatibility of the return types of unapplySeq
25+
val x :: Nil = R.unapplySeq(matchh: Any).get
26+
val y :: Nil = R.unapplySeq(matchh).get
27+
assertEquals("1", x)
28+
assertEquals("1", y)
29+
}
30+
}

0 commit comments

Comments
 (0)