Skip to content

Commit d71f702

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

36 files changed

+94
-82
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/Course-2002-03.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object M0 {
1515
r.denom * s.denom);
1616

1717
def makeString(r: Rational) =
18-
r.numer + "/" + r.denom;
18+
s"${r.numer}/${r.denom}"
1919

2020
val x = new Rational(1, 2);
2121
val y = new Rational(1, 3);
@@ -40,7 +40,7 @@ object M1 {
4040
new Rational(
4141
numer * r.numer,
4242
denom * r.denom);
43-
override def toString() = numer + "/" + denom;
43+
override def toString() = s"$numer/$denom"
4444
}
4545

4646
val x = new Rational(1, 3);
@@ -77,7 +77,7 @@ object M2 {
7777
new Rational(
7878
numer * r.denom,
7979
denom * r.numer);
80-
override def toString() = numer + "/" + denom;
80+
override def toString() = s"$numer/$denom"
8181
}
8282

8383
val x = new Rational(1, 3);
@@ -100,7 +100,7 @@ object M3 {
100100
def less(that: Rational) =
101101
this.numer * that.denom < that.numer * this.denom;
102102
def max(that: Rational) = if (this.less(that)) that else this;
103-
override def toString() = numer + "/" + denom;
103+
override def toString() = s"$numer/$denom"
104104
}
105105

106106
val x = new Rational(66, 42);
@@ -136,7 +136,7 @@ object M4 {
136136
new Rational(
137137
numer * r.denom,
138138
denom * r.numer);
139-
override def toString() = numer + "/" + denom;
139+
override def toString() = s"$numer/$denom"
140140
}
141141

142142
val x = new Rational(1, 2);

test/files/run/Course-2002-06.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class PostScript (filename: String, _width: Double, _height: Double)
122122
}
123123

124124
def plotLine(x1: Double, y1: Double, x2: Double, y2: Double): Unit = {
125-
Console.println(round(x1) + " " + round(y1) + " m " +
125+
Console.println(round(x1).toString + " " + round(y1) + " m " +
126126
round(x2) + " " + round(y2) + " l");
127127
}
128128

@@ -132,7 +132,7 @@ class PostScript (filename: String, _width: Double, _height: Double)
132132
Console.println("%%BoundingBox: 0 0 " + mm2ps(psWidth) + " " + mm2ps(psHeight));
133133
Console.println("%%EndComments\n");
134134
Console.println("/m {moveto} bind def\n/l {lineto} bind def\n");
135-
Console.println(mm2ps(line_thickness) + " setlinewidth\nnewpath");
135+
Console.println(mm2ps(line_thickness).toString + " setlinewidth\nnewpath");
136136

137137
/** Terminate the PS document and close the file stream. */
138138
def close : Unit = {

test/files/run/Course-2002-08.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ object M3 {
135135
object M4 {
136136

137137
def test = {
138-
for (i <- range(1, 4)) { Console.print(i + " ") };
138+
for (i <- range(1, 4)) { Console.print(s"$i ") };
139139
Console.println;
140140
Console.println(for (i <- range(1, 4)) yield i);
141141
Console.println;
@@ -295,7 +295,7 @@ object M5 {
295295
ain setSignal (if (a == 0) false else true);
296296
bin setSignal (if (b == 0) false else true);
297297
run;
298-
Console.println(a + " & " + b + " = " + result);
298+
Console.println(a.toString + " & " + b + " = " + result);
299299
Console.println;
300300
}
301301

@@ -320,7 +320,7 @@ object M5 {
320320
ain setSignal (if (a == 0) false else true);
321321
bin setSignal (if (b == 0) false else true);
322322
run;
323-
Console.println(a + " | " + b + " = " + result);
323+
Console.println(a.toString + " | " + b + " = " + result);
324324
Console.println;
325325
}
326326

@@ -348,7 +348,7 @@ object M5 {
348348
ain setSignal (if (a == 0) false else true);
349349
bin setSignal (if (b == 0) false else true);
350350
run;
351-
Console.println(a + " + " + b + " = " + result);
351+
Console.println(a.toString + " + " + b + " = " + result);
352352
Console.println;
353353
}
354354

@@ -379,7 +379,7 @@ object M5 {
379379
bin setSignal (if (b == 0) false else true);
380380
cin setSignal (if (c == 0) false else true);
381381
run;
382-
Console.println(a + " + " + b + " + " + c + " = " + result);
382+
Console.println(a.toString + " + " + b + " + " + c + " = " + result);
383383
Console.println;
384384
}
385385

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(xi4)))
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: 9 additions & 9 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,13 +406,13 @@ 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) = {
415-
def inner(b: Int = a, c: String) = println(b +": "+ c)
415+
def inner(b: Int = a, c: String) = println(b.toString +": "+ c)
416416
inner(c = "/")
417417
}
418418
def test5(argName: Unit) = println("test5")
@@ -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

0 commit comments

Comments
 (0)