Skip to content

Commit 06e1c15

Browse files
authored
Merge pull request scala/scala#7041 from dwijnand/WrappedString-private-self
Make WrappedString keep its self private
2 parents 76113a0 + b3cfa44 commit 06e1c15

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

library/src/scala/collection/StringOps.scala

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,8 +1325,8 @@ final class StringOps(private val s: String) extends AnyVal {
13251325
* ''n'' times in `that`, then the first ''n'' occurrences of `x` will not form
13261326
* part of the result, but any following occurrences will.
13271327
*/
1328-
@deprecated("Use `new WrappedString(s).diff(...).self` instead of `s.diff(...)`", "2.13.0")
1329-
def diff(that: Seq[_ >: Char]): String = new WrappedString(s).diff(that).self
1328+
@deprecated("Use `new WrappedString(s).diff(...).unwrap` instead of `s.diff(...)`", "2.13.0")
1329+
def diff(that: Seq[_ >: Char]): String = new WrappedString(s).diff(that).unwrap
13301330

13311331
/** Computes the multiset intersection between this string and another sequence.
13321332
*
@@ -1337,12 +1337,12 @@ final class StringOps(private val s: String) extends AnyVal {
13371337
* ''n'' times in `that`, then the first ''n'' occurrences of `x` will be retained
13381338
* in the result, but any following occurrences will be omitted.
13391339
*/
1340-
@deprecated("Use `new WrappedString(s).intersect(...).self` instead of `s.intersect(...)`", "2.13.0")
1341-
def intersect(that: Seq[_ >: Char]): String = new WrappedString(s).intersect(that).self
1340+
@deprecated("Use `new WrappedString(s).intersect(...).unwrap` instead of `s.intersect(...)`", "2.13.0")
1341+
def intersect(that: Seq[_ >: Char]): String = new WrappedString(s).intersect(that).unwrap
13421342

13431343
/** Selects all distinct chars of this string ignoring the duplicates. */
1344-
@deprecated("Use `new WrappedString(s).distinct.self` instead of `s.distinct`", "2.13.0")
1345-
def distinct: String = new WrappedString(s).distinct.self
1344+
@deprecated("Use `new WrappedString(s).distinct.unwrap` instead of `s.distinct`", "2.13.0")
1345+
def distinct: String = new WrappedString(s).distinct.unwrap
13461346

13471347
/** Selects all distinct chars of this string ignoring the duplicates as determined by `==` after applying
13481348
* the transforming function `f`.
@@ -1351,8 +1351,8 @@ final class StringOps(private val s: String) extends AnyVal {
13511351
* @tparam B the type of the elements after being transformed by `f`
13521352
* @return a new string consisting of all the chars of this string without duplicates.
13531353
*/
1354-
@deprecated("Use `new WrappedString(s).distinctBy(...).self` instead of `s.distinctBy(...)`", "2.13.0")
1355-
def distinctBy[B](f: Char => B): String = new WrappedString(s).distinctBy(f).self
1354+
@deprecated("Use `new WrappedString(s).distinctBy(...).unwrap` instead of `s.distinctBy(...)`", "2.13.0")
1355+
def distinctBy[B](f: Char => B): String = new WrappedString(s).distinctBy(f).unwrap
13561356

13571357
/** Sorts the characters of this string according to an Ordering.
13581358
*
@@ -1365,8 +1365,8 @@ final class StringOps(private val s: String) extends AnyVal {
13651365
* @return a string consisting of the chars of this string
13661366
* sorted according to the ordering `ord`.
13671367
*/
1368-
@deprecated("Use `new WrappedString(s).sorted.self` instead of `s.sorted`", "2.13.0")
1369-
def sorted[B >: Char](implicit ord: Ordering[B]): String = new WrappedString(s).sorted(ord).self
1368+
@deprecated("Use `new WrappedString(s).sorted.unwrap` instead of `s.sorted`", "2.13.0")
1369+
def sorted[B >: Char](implicit ord: Ordering[B]): String = new WrappedString(s).sorted(ord).unwrap
13701370

13711371
/** Sorts this string according to a comparison function.
13721372
*
@@ -1379,8 +1379,8 @@ final class StringOps(private val s: String) extends AnyVal {
13791379
* @return a string consisting of the elements of this string
13801380
* sorted according to the comparison function `lt`.
13811381
*/
1382-
@deprecated("Use `new WrappedString(s).sortWith(...).self` instead of `s.sortWith(...)`", "2.13.0")
1383-
def sortWith(lt: (Char, Char) => Boolean): String = new WrappedString(s).sortWith(lt).self
1382+
@deprecated("Use `new WrappedString(s).sortWith(...).unwrap` instead of `s.sortWith(...)`", "2.13.0")
1383+
def sortWith(lt: (Char, Char) => Boolean): String = new WrappedString(s).sortWith(lt).unwrap
13841384

13851385
/** Sorts this string according to the Ordering which results from transforming
13861386
* an implicitly given Ordering with a transformation function.
@@ -1398,8 +1398,8 @@ final class StringOps(private val s: String) extends AnyVal {
13981398
* sorted according to the ordering where `x < y` if
13991399
* `ord.lt(f(x), f(y))`.
14001400
*/
1401-
@deprecated("Use `new WrappedString(s).sortBy(...).self` instead of `s.sortBy(...)`", "2.13.0")
1402-
def sortBy[B](f: Char => B)(implicit ord: Ordering[B]): String = new WrappedString(s).sortBy(f)(ord).self
1401+
@deprecated("Use `new WrappedString(s).sortBy(...).unwrap` instead of `s.sortBy(...)`", "2.13.0")
1402+
def sortBy[B](f: Char => B)(implicit ord: Ordering[B]): String = new WrappedString(s).sortBy(f)(ord).unwrap
14031403

14041404
/** Partitions this string into a map of strings according to some discriminator function.
14051405
*
@@ -1413,8 +1413,8 @@ final class StringOps(private val s: String) extends AnyVal {
14131413
* for which `f(x)` equals `k`.
14141414
*
14151415
*/
1416-
@deprecated("Use `new WrappedString(s).groupBy(...).view.mapValues(_.self)` instead of `s.groupBy(...)`", "2.13.0")
1417-
def groupBy[K](f: Char => K): immutable.Map[K, String] = new WrappedString(s).groupBy(f).view.mapValues(_.self).toMap
1416+
@deprecated("Use `new WrappedString(s).groupBy(...).view.mapValues(_.unwrap)` instead of `s.groupBy(...)`", "2.13.0")
1417+
def groupBy[K](f: Char => K): immutable.Map[K, String] = new WrappedString(s).groupBy(f).view.mapValues(_.unwrap).toMap
14181418

14191419
/** Groups chars in fixed size blocks by passing a "sliding window"
14201420
* over them (as opposed to partitioning them, as is done in grouped.)
@@ -1426,8 +1426,8 @@ final class StringOps(private val s: String) extends AnyVal {
14261426
* last element (which may be the only element) will be truncated
14271427
* if there are fewer than `size` chars remaining to be grouped.
14281428
*/
1429-
@deprecated("Use `new WrappedString(s).sliding(...).map(_.self)` instead of `s.sliding(...)`", "2.13.0")
1430-
def sliding(size: Int, step: Int = 1): Iterator[String] = new WrappedString(s).sliding(size, step).map(_.self)
1429+
@deprecated("Use `new WrappedString(s).sliding(...).map(_.unwrap)` instead of `s.sliding(...)`", "2.13.0")
1430+
def sliding(size: Int, step: Int = 1): Iterator[String] = new WrappedString(s).sliding(size, step).map(_.unwrap)
14311431

14321432
/** Iterates over combinations. A _combination_ of length `n` is a subsequence of
14331433
* the original string, with the chars taken in order. Thus, `"xy"` and `"yy"`
@@ -1442,16 +1442,16 @@ final class StringOps(private val s: String) extends AnyVal {
14421442
* @return An Iterator which traverses the possible n-element combinations of this string.
14431443
* @example `"abbbc".combinations(2) = Iterator(ab, ac, bb, bc)`
14441444
*/
1445-
@deprecated("Use `new WrappedString(s).combinations(...).map(_.self)` instead of `s.combinations(...)`", "2.13.0")
1446-
def combinations(n: Int): Iterator[String] = new WrappedString(s).combinations(n).map(_.self)
1445+
@deprecated("Use `new WrappedString(s).combinations(...).map(_.unwrap)` instead of `s.combinations(...)`", "2.13.0")
1446+
def combinations(n: Int): Iterator[String] = new WrappedString(s).combinations(n).map(_.unwrap)
14471447

14481448
/** Iterates over distinct permutations.
14491449
*
14501450
* @return An Iterator which traverses the distinct permutations of this string.
14511451
* @example `"abb".permutations = Iterator(abb, bab, bba)`
14521452
*/
1453-
@deprecated("Use `new WrappedString(s).permutations(...).map(_.self)` instead of `s.permutations(...)`", "2.13.0")
1454-
def permutations: Iterator[String] = new WrappedString(s).permutations.map(_.self)
1453+
@deprecated("Use `new WrappedString(s).permutations(...).map(_.unwrap)` instead of `s.permutations(...)`", "2.13.0")
1454+
def permutations: Iterator[String] = new WrappedString(s).permutations.map(_.unwrap)
14551455
}
14561456

14571457
case class StringView(s: String) extends AbstractIndexedSeqView[Char] {

library/src/scala/collection/immutable/WrappedString.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import mutable.{Builder, StringBuilder}
1717
* @define Coll `WrappedString`
1818
* @define coll wrapped string
1919
*/
20-
final class WrappedString(val self: String) extends AbstractSeq[Char] with IndexedSeq[Char]
20+
final class WrappedString(private val self: String) extends AbstractSeq[Char] with IndexedSeq[Char]
2121
with IndexedSeqOps[Char, IndexedSeq, WrappedString] {
2222

2323
def apply(i: Int): Char = self.charAt(i)
@@ -64,4 +64,8 @@ object WrappedString extends SpecificIterableFactory[Char, WrappedString] {
6464
val empty: WrappedString = new WrappedString("")
6565
def newBuilder: Builder[Char, WrappedString] =
6666
new StringBuilder().mapResult(x => new WrappedString(x))
67+
68+
implicit class UnwrapOp(private val value: WrappedString) extends AnyVal {
69+
def unwrap: String = value.self
70+
}
6771
}

library/src/scala/collection/mutable/StringBuilder.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ final class StringBuilder(val underlying: java.lang.StringBuilder) extends Abstr
158158
*/
159159
def appendAll(xs: IterableOnce[Char]): StringBuilder = {
160160
xs match {
161-
case x: WrappedString => underlying append x.self
161+
case x: WrappedString => underlying append x.unwrap
162162
case x: ArraySeq.ofChar => underlying append x.array
163163
case x: StringBuilder => underlying append x.underlying
164164
case _ =>

0 commit comments

Comments
 (0)