Skip to content

Commit 0ec9475

Browse files
committed
Deprecate +(x: String): String on primitives
This is consistent with the deprecation of any2stringadd in scala#6315.
1 parent 235e69d commit 0ec9475

26 files changed

+69
-57
lines changed

project/GenerateAnyVals.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ import scala.language.implicitConversions"""
146146

147147
def mkCoercions = numeric map (x => "def to%s: %s".format(x, x))
148148
def mkUnaryOps = unaryOps map (x => "%s\n def unary_%s : %s".format(x.doc, x.op, this opType I))
149-
def mkStringOps = List("def +(x: String): String")
149+
def mkStringOps = List(
150+
"""@deprecated("Adding a number and a String is deprecated. Convert the number to a String with `toString` first to call +", "2.13.0") def +(x: String): String"""
151+
)
150152
def mkShiftOps = (
151153
for (op <- shiftOps ; arg <- List(I, L)) yield
152154
"%s\n def %s(x: %s): %s".format(op.doc, op.op, arg, this opType I)

src/library/scala/Byte.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ final abstract class Byte private extends AnyVal {
4242
/** Returns the negation of this value. */
4343
def unary_- : Int
4444

45-
def +(x: String): String
45+
@deprecated("Adding a number and a String is deprecated. Convert the number to a String with `toString` first to call +", "2.13.0") def +(x: String): String
4646

4747
/**
4848
* Returns this value bit-shifted left by the specified number of bits,

src/library/scala/Char.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ final abstract class Char private extends AnyVal {
4242
/** Returns the negation of this value. */
4343
def unary_- : Int
4444

45-
def +(x: String): String
45+
@deprecated("Adding a number and a String is deprecated. Convert the number to a String with `toString` first to call +", "2.13.0") def +(x: String): String
4646

4747
/**
4848
* Returns this value bit-shifted left by the specified number of bits,

src/library/scala/Double.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final abstract class Double private extends AnyVal {
3333
/** Returns the negation of this value. */
3434
def unary_- : Double
3535

36-
def +(x: String): String
36+
@deprecated("Adding a number and a String is deprecated. Convert the number to a String with `toString` first to call +", "2.13.0") def +(x: String): String
3737

3838
/** Returns `true` if this value is equal to x, `false` otherwise. */
3939
def ==(x: Byte): Boolean

src/library/scala/Float.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final abstract class Float private extends AnyVal {
3333
/** Returns the negation of this value. */
3434
def unary_- : Float
3535

36-
def +(x: String): String
36+
@deprecated("Adding a number and a String is deprecated. Convert the number to a String with `toString` first to call +", "2.13.0") def +(x: String): String
3737

3838
/** Returns `true` if this value is equal to x, `false` otherwise. */
3939
def ==(x: Byte): Boolean

src/library/scala/Int.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ final abstract class Int private extends AnyVal {
4242
/** Returns the negation of this value. */
4343
def unary_- : Int
4444

45-
def +(x: String): String
45+
@deprecated("Adding a number and a String is deprecated. Convert the number to a String with `toString` first to call +", "2.13.0") def +(x: String): String
4646

4747
/**
4848
* Returns this value bit-shifted left by the specified number of bits,

src/library/scala/Long.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ final abstract class Long private extends AnyVal {
4242
/** Returns the negation of this value. */
4343
def unary_- : Long
4444

45-
def +(x: String): String
45+
@deprecated("Adding a number and a String is deprecated. Convert the number to a String with `toString` first to call +", "2.13.0") def +(x: String): String
4646

4747
/**
4848
* Returns this value bit-shifted left by the specified number of bits,

src/library/scala/Short.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ final abstract class Short private extends AnyVal {
4242
/** Returns the negation of this value. */
4343
def unary_- : Int
4444

45-
def +(x: String): String
45+
@deprecated("Adding a number and a String is deprecated. Convert the number to a String with `toString` first to call +", "2.13.0") def +(x: String): String
4646

4747
/**
4848
* Returns this value bit-shifted left by the specified number of bits,

test/files/jvm/duration-tck.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ object Test extends App {
151151
intercept[IllegalArgumentException] { mdur / 0.9 }
152152
intercept[IllegalArgumentException] { dur / 0.4 }
153153
intercept[IllegalArgumentException] { mdur / 0.4 }
154-
Duration(x + unit.toString.toLowerCase)
154+
Duration(x.toString + unit.toString.toLowerCase)
155155
Duration("-" + x + unit.toString.toLowerCase)
156156
intercept[IllegalArgumentException] { Duration("%.0f".format(x + 10000000d) + unit.toString.toLowerCase) }
157157
intercept[IllegalArgumentException] { Duration("-%.0f".format(x + 10000000d) + unit.toString.toLowerCase) }

test/files/jvm/sync-var.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ summers foreach { _.join() }
4444

4545
val got = sum.get
4646
val expected = (n + 1) * n / 2
47-
println(got + " " + expected + " " + (got == expected))
47+
println(got.toString + " " + expected + " " + (got == expected))
4848

4949
producers foreach { _.join() }
5050

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
numeric-add-string-warning.scala:2: warning: method + in class Int is deprecated (since 2.13.0): Adding a number and a String is deprecated. Convert the number to a String with `toString` first to call +
2+
val x = 4 + "2"
3+
^
4+
error: No warnings can be incurred under -Xfatal-warnings.
5+
one warning found
6+
one error found
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Xfatal-warnings -deprecation
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test {
2+
val x = 4 + "2"
3+
}

test/files/run/bridges.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7116,7 +7116,7 @@ object Test {
71167116

71177117
if (errors > 0) {
71187118
Console.println;
7119-
Console.println(errors + " error" + (if (errors > 1) "s" else ""));
7119+
Console.println(s"$errors error" + (if (errors > 1) "s" else ""));
71207120
}
71217121
}
71227122
}

test/files/run/bugs.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ object Test {
481481

482482
if (errors > 0) {
483483
Console.println;
484-
Console.println(errors + " error" + (if (errors > 1) "s" else ""));
484+
Console.println(s"$errors error" + (if (errors > 1) "s" else ""));
485485
}
486486
}
487487
}

test/files/run/fors.scala

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,29 @@ object Test extends App {
1818
println("\ntestOld")
1919

2020
// lists
21-
for (x <- xs) print(x + " "); println
21+
for (x <- xs) print(s"$x "); println
2222
for (x <- xs;
23-
if x % 2 == 0) print(x + " "); println
23+
if x % 2 == 0) print(s"$x "); println
2424
for {x <- xs
25-
if x % 2 == 0} print(x + " "); println
25+
if x % 2 == 0} print(s"$x "); println
2626
var n = 0
2727
for (_ <- xs) n += 1; println(n)
28-
for ((x, y) <- xs zip ys) print(x + " "); println
29-
for (p @ (x, y) <- xs zip ys) print(p._1 + " "); println
28+
for ((x, y) <- xs zip ys) print(s"$x "); println
29+
for (p @ (x, y) <- xs zip ys) print(s"${p._1} "); println
3030

3131
// iterators
32-
for (x <- it) print(x + " "); println
32+
for (x <- it) print(s"$x "); println
3333
for (x <- it;
34-
if x % 2 == 0) print(x + " "); println
34+
if x % 2 == 0) print(s"$x "); println
3535
for {x <- it
36-
if x % 2 == 0} print(x + " "); println
36+
if x % 2 == 0} print(s"$x "); println
3737

3838
// arrays
39-
for (x <- ar) print(x + " "); println
39+
for (x <- ar) print(s"$x "); println
4040
for (x <- ar;
41-
if x.toInt > 97) print(x + " "); println
41+
if x.toInt > 97) print(s"$x "); println
4242
for {x <- ar
43-
if x.toInt > 97} print(x + " "); println
43+
if x.toInt > 97} print(s"$x "); println
4444

4545
}
4646

@@ -52,28 +52,28 @@ object Test extends App {
5252
// lists
5353
var n = 0
5454
for (_ <- xs) n += 1; println(n)
55-
for ((x, y) <- xs zip ys) print(x + " "); println
56-
for (p @ (x, y) <- xs zip ys) print(p._1 + " "); println
55+
for ((x, y) <- xs zip ys) print(s"$x "); println
56+
for (p @ (x, y) <- xs zip ys) print(s"${p._1} "); println
5757

5858
// iterators
59-
for (x <- it) print(x + " "); println
60-
for (x <- it if x % 2 == 0) print(x + " "); println
61-
for (x <- it; if x % 2 == 0) print(x + " "); println
59+
for (x <- it) print(s"$x "); println
60+
for (x <- it if x % 2 == 0) print(s"$x "); println
61+
for (x <- it; if x % 2 == 0) print(s"$x "); println
6262
for (x <- it;
63-
if x % 2 == 0) print(x + " "); println
63+
if x % 2 == 0) print(s"$x "); println
6464
for (x <- it
65-
if x % 2 == 0) print(x + " "); println
65+
if x % 2 == 0) print(s"$x "); println
6666
for {x <- it
67-
if x % 2 == 0} print(x + " "); println
67+
if x % 2 == 0} print(s"$x "); println
6868
for (x <- it;
6969
y = 2
70-
if x % y == 0) print(x + " "); println
70+
if x % y == 0) print(s"$x "); println
7171
for {x <- it
7272
y = 2
73-
if x % y == 0} print(x + " "); println
73+
if x % y == 0} print(s"$x "); println
7474

7575
// arrays
76-
for (x <- ar) print(x + " "); println
76+
for (x <- ar) print(s"$x "); println
7777

7878
}
7979

test/files/run/kmpSliceSearch.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ object Test {
5454
)
5555
(haystacks zip needles) foreach {
5656
case (hay, nee) =>
57-
println(hay.indexOfSlice(nee,2) + " " + hay.lastIndexOfSlice(nee,13))
57+
println(s"${hay.indexOfSlice(nee,2)} ${hay.lastIndexOfSlice(nee,13)}")
5858
}
5959
}
6060
}

test/files/run/macro-auto-duplicate/Macros_1.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ object Macros {
88
def defAndUseX(rhs: Tree) = {
99
Block(List(ValDef(NoMods, newTermName("x"), TypeTree(), rhs)), x)
1010
}
11-
val xi4 = defAndUseX(Literal(Constant(4)))
12-
val xs2 = defAndUseX(Literal(Constant("2")))
13-
c.Expr[String](Apply(Select(xi4, newTermName("$plus")), List(xs2)))
11+
val xs2 = defAndUseX(Literal(Constant("4")))
12+
val xi4 = defAndUseX(Literal(Constant(2)))
13+
c.Expr[String](Apply(Select(xs2, newTermName("$plus")), List(x4)))
1414
}
1515

1616
def foo = macro impl
17-
}
17+
}

test/files/run/map_test.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object Test extends App {
3030
i = 0
3131
while(i < 4712) {
3232
if (map.isDefinedAt(i))
33-
print(i + "->" + map(i) + " ");
33+
print(s"$i->${map(i)} ")
3434
i += 1
3535
}
3636
println("")

test/files/run/names-defaults.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ object Test extends App {
119119

120120

121121
// vararg
122-
def test5(a: Int, b: Int)(c: Int, d: String*) = a +", "+ d.toList
122+
def test5(a: Int, b: Int)(c: Int, d: String*) = s"$a, ${d.toList}"
123123
println(test5(b = 1, a = 2)(3, "4", "4", "4"))
124124
println(test5(b = 1, a = 2)(c = 29))
125125

126126

127127
// tuple conversion
128-
def foo(a: Int, b: Int)(c: (Int, String)) = a + c._1
128+
def foo(a: Int, b: Int)(c: (Int, String)) = s"$a${c._1}"
129129
println(foo(b = 1, a = 2)(3, "4"))
130130

131131

@@ -406,10 +406,10 @@ object Test extends App {
406406
println(f8177(a = 1, 1))
407407

408408
// DEFINITIONS
409-
def test1(a: Int, b: String) = println(a +": "+ b)
410-
def test2(u: Int, v: Int)(k: String, l: Int) = println(l +": "+ k +", "+ (u + v))
409+
def test1(a: Int, b: String) = println(s"$a: $b")
410+
def test2(u: Int, v: Int)(k: String, l: Int) = println(l.toString +": "+ k +", "+ (u + v))
411411

412-
def test3[T1, T2](a: Int, b: T1)(c: String, d: T2) = println(a +": "+ c +", "+ b +", "+ d)
412+
def test3[T1, T2](a: Int, b: T1)(c: String, d: T2) = println(a.toString +": "+ c +", "+ b +", "+ d)
413413

414414
def test4(a: Int) = {
415415
def inner(b: Int = a, c: String) = println(b +": "+ c)
@@ -425,13 +425,13 @@ object Test extends App {
425425

426426

427427
class Base {
428-
def test1[T1, T2](a: Int = 100, b: T1)(c: T2, d: String = a +": "+ b)(e: T2 = c, f: Int) =
429-
println(a +": "+ d +", "+ b +", "+ c +", "+ e +", "+ f)
428+
def test1[T1, T2](a: Int = 100, b: T1)(c: T2, d: String = a.toString +": "+ b)(e: T2 = c, f: Int) =
429+
println(a.toString +": "+ d +", "+ b +", "+ c +", "+ e +", "+ f)
430430
}
431431

432432
class Sub1 extends Base {
433433
override def test1[U1, U2](b: Int, a: U1)(m: U2, r: String = "overridden")(o: U2, f: Int = 555) =
434-
println(b +": "+ r +", "+ a +", "+ m +", "+ o +", "+ f)
434+
println(b.toString +": "+ r +", "+ a +", "+ m +", "+ o +", "+ f)
435435
}
436436

437437

test/files/run/runtime.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ object Test {
191191

192192
if (errors > 0) {
193193
Console.println;
194-
Console.println(errors + " error" + (if (errors > 1) "s" else ""));
194+
Console.println(s"$errors error" + (if (errors > 1) "s" else ""));
195195
}
196196
}
197197
}

test/files/run/t3346e.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ object Test extends App {
6868
println("qwe".filterMap((c: Char) => Some(c.toInt)))
6969
println("qwe".filterMap((c: Char) => Some(c)))
7070
println(Array(2, 0).filterMap((c: Int) => Some(c.toInt)).toList)
71-
println(Seq(2, 0).filterMap((c: Int) => if (c < 2) Some(c + "!") else None))
71+
println(Seq(2, 0).filterMap((c: Int) => if (c < 2) Some(s"$c!") else None))
7272
def test(i:Int) = Option(i)
7373
println(BitSet(2,0).filterMap(test))
7474

test/files/run/t5356.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import scala.language.{ reflectiveCalls }
33
object Test {
4-
def f(x: Any { def toInt: Int }) = println(x.toInt + " " + x.getClass.getName)
4+
def f(x: Any { def toInt: Int }) = println(x.toInt.toString + " " + x.getClass.getName)
55

66
def main(args: Array[String]): Unit = {
77
f(1)

test/files/run/t6928-run.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
abstract class A( val someAs: A* ) {
2-
override def toString = someAs.length + " As"
2+
override def toString = someAs.length.toString + " As"
33
}
44
object B extends A(null, null, null)
55

test/files/run/unittest_iterator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Test {
1313
for (i <- it) {
1414
// sum of the groups == sum of the original
1515
val thisSum = ((it grouped i) map (_.sum)).to(LazyList).sum
16-
assert(thisSum == itSum, thisSum + " != " + itSum)
16+
assert(thisSum == itSum, s"$thisSum != $itSum" )
1717
}
1818

1919
// grouped

test/files/run/vector1.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object Test {
4242

4343
def assertVectorIndexed[V](a: Vector[V], label: String, start: Int, end: Int) = {
4444
val res = a
45-
assert(res.length == (end-start), res.length+"!="+(end-start)+" ("+res+")")
45+
assert(res.length == (end-start), res.length.toString+"!="+(end-start)+" ("+res+")")
4646
for (i <- start until end) {
4747
assert(res(i) == (label + i), ""+res(i)+"!="+(label + i))
4848
}
@@ -51,7 +51,7 @@ object Test {
5151

5252
def assertVectorIterated[V](a: Vector[V], label: String, start: Int, end: Int) = {
5353
val res = a
54-
assert(res.length == (end-start), res.length+"!="+(end-start)+" ("+res+")")
54+
assert(res.length == (end-start), res.length.toString+"!="+(end-start)+" ("+res+")")
5555
var i = start
5656
var it = res.iterator
5757
while(it.hasNext) {
@@ -146,7 +146,7 @@ object Test {
146146
var i = 0
147147
var it = a
148148
while (i < N) {
149-
assert(it.length == (N-i), it.length+" items at iteration "+i)
149+
assert(it.length == (N-i), it.length.toString+" items at iteration "+i)
150150
val x = it(0)
151151
val y = it(N-i-1)
152152
assert(x == "a"+i, x+"!=a"+i)
@@ -170,7 +170,7 @@ object Test {
170170
var i = 0
171171
var it = a
172172
while (i < N) {
173-
assert(it.length == (N-i), it.length+" items at iteration "+i)
173+
assert(it.length == (N-i), it.length.toString+" items at iteration "+i)
174174
val x = it(0)
175175
val y = it(N-i-1)
176176
// println("x " + x + "/" + i)

0 commit comments

Comments
 (0)