Skip to content

Commit ff442fc

Browse files
committed
SI-6710 Clarify stub methods in primitive value classes
- Replaces the implementations of box/unbox in AnyVal companions by `???`, the methods are only stubs, and the impls did not correspond to the actual behavior. The doc comment already points to the actual implementation in BoxesRunTime. - Replaces the body of `getClass` from `null` to `???` and clarifies in a comment why the overrides exist.
1 parent 19dac82 commit ff442fc

File tree

10 files changed

+47
-39
lines changed

10 files changed

+47
-39
lines changed

src/compiler/scala/tools/cmd/gen/AnyVals.scala

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ import scala.language.implicitConversions"""
200200
def classLines: List[String]
201201
def objectLines: List[String]
202202
def commonClassLines = List(
203-
"override def getClass(): Class[@name@] = null"
203+
"// Provide a more specific return type for Scaladoc",
204+
"override def getClass(): Class[@name@] = ???"
204205
)
205206

206207
def lcname = name.toLowerCase
@@ -225,15 +226,13 @@ import scala.language.implicitConversions"""
225226
def indent(s: String) = if (s == "") "" else " " + s
226227
def indentN(s: String) = s.lines map indent mkString "\n"
227228

228-
def boxUnboxImpls = Map(
229+
def boxUnboxInterpolations = Map(
229230
"@boxRunTimeDoc@" -> """
230231
* Runtime implementation determined by `scala.runtime.BoxesRunTime.boxTo%s`. See [[https://github.com/scala/scala src/library/scala/runtime/BoxesRunTime.java]].
231232
*""".format(boxedSimpleName),
232-
"@boxImpl@" -> "%s.valueOf(x)".format(boxedName),
233233
"@unboxRunTimeDoc@" -> """
234234
* Runtime implementation determined by `scala.runtime.BoxesRunTime.unboxTo%s`. See [[https://github.com/scala/scala src/library/scala/runtime/BoxesRunTime.java]].
235235
*""".format(name),
236-
"@unboxImpl@" -> "x.asInstanceOf[%s].%sValue()".format(boxedName, lcname),
237236
"@unboxDoc@" -> "the %s resulting from calling %sValue() on `x`".format(name, lcname)
238237
)
239238
def interpolations = Map(
@@ -243,7 +242,7 @@ import scala.language.implicitConversions"""
243242
"@boxed@" -> boxedName,
244243
"@lcname@" -> lcname,
245244
"@zero@" -> zeroRep
246-
) ++ boxUnboxImpls
245+
) ++ boxUnboxInterpolations
247246

248247
def interpolate(s: String): String = interpolations.foldLeft(s) {
249248
case (str, (key, value)) => str.replaceAll(key, value)
@@ -305,7 +304,7 @@ package scala
305304
* @param x the @name@ to be boxed
306305
* @return a @boxed@ offering `x` as its underlying value.
307306
*/
308-
def box(x: @name@): @boxed@ = @boxImpl@
307+
def box(x: @name@): @boxed@ = ???
309308
310309
/** Transform a boxed type into a value type. Note that this
311310
* method is not typesafe: it accepts any Object, but will throw
@@ -315,7 +314,7 @@ def box(x: @name@): @boxed@ = @boxImpl@
315314
* @throws ClassCastException if the argument is not a @boxed@
316315
* @return @unboxDoc@
317316
*/
318-
def unbox(x: java.lang.Object): @name@ = @unboxImpl@
317+
def unbox(x: java.lang.Object): @name@ = ???
319318
320319
/** The String representation of the scala.@name@ companion object. */
321320
override def toString = "object scala.@name@"
@@ -444,7 +443,8 @@ def &(x: Boolean): Boolean
444443
*/
445444
def ^(x: Boolean): Boolean
446445
447-
override def getClass(): Class[Boolean] = null
446+
// Provide a more specific return type for Scaladoc
447+
override def getClass(): Class[Boolean] = ???
448448
""".trim.lines.toList
449449

450450
def objectLines = interpolate(allCompanions + "\n" + nonUnitCompanions).lines.toList
@@ -458,15 +458,14 @@ override def getClass(): Class[Boolean] = null
458458
*/
459459
"""
460460
def classLines = List(
461-
"""override def getClass(): Class[Unit] = null"""
461+
"// Provide a more specific return type for Scaladoc",
462+
"override def getClass(): Class[Unit] = ???"
462463
)
463464
def objectLines = interpolate(allCompanions).lines.toList
464465

465-
override def boxUnboxImpls = Map(
466+
override def boxUnboxInterpolations = Map(
466467
"@boxRunTimeDoc@" -> "",
467-
"@boxImpl@" -> "scala.runtime.BoxedUnit.UNIT",
468468
"@unboxRunTimeDoc@" -> "",
469-
"@unboxImpl@" -> "()",
470469
"@unboxDoc@" -> "the Unit value ()"
471470
)
472471
}

src/library/scala/Boolean.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ final abstract class Boolean private extends AnyVal {
102102
*/
103103
def ^(x: Boolean): Boolean
104104

105-
override def getClass(): Class[Boolean] = null
105+
// Provide a more specific return type for Scaladoc
106+
override def getClass(): Class[Boolean] = ???
106107
}
107108

108109
object Boolean extends AnyValCompanion {
@@ -114,7 +115,7 @@ object Boolean extends AnyValCompanion {
114115
* @param x the Boolean to be boxed
115116
* @return a java.lang.Boolean offering `x` as its underlying value.
116117
*/
117-
def box(x: Boolean): java.lang.Boolean = java.lang.Boolean.valueOf(x)
118+
def box(x: Boolean): java.lang.Boolean = ???
118119

119120
/** Transform a boxed type into a value type. Note that this
120121
* method is not typesafe: it accepts any Object, but will throw
@@ -126,7 +127,7 @@ object Boolean extends AnyValCompanion {
126127
* @throws ClassCastException if the argument is not a java.lang.Boolean
127128
* @return the Boolean resulting from calling booleanValue() on `x`
128129
*/
129-
def unbox(x: java.lang.Object): Boolean = x.asInstanceOf[java.lang.Boolean].booleanValue()
130+
def unbox(x: java.lang.Object): Boolean = ???
130131

131132
/** The String representation of the scala.Boolean companion object. */
132133
override def toString = "object scala.Boolean"

src/library/scala/Byte.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ final abstract class Byte private extends AnyVal {
434434
/** Returns the remainder of the division of this value by `x`. */
435435
def %(x: Double): Double
436436

437-
override def getClass(): Class[Byte] = null
437+
// Provide a more specific return type for Scaladoc
438+
override def getClass(): Class[Byte] = ???
438439
}
439440

440441
object Byte extends AnyValCompanion {
@@ -451,7 +452,7 @@ object Byte extends AnyValCompanion {
451452
* @param x the Byte to be boxed
452453
* @return a java.lang.Byte offering `x` as its underlying value.
453454
*/
454-
def box(x: Byte): java.lang.Byte = java.lang.Byte.valueOf(x)
455+
def box(x: Byte): java.lang.Byte = ???
455456

456457
/** Transform a boxed type into a value type. Note that this
457458
* method is not typesafe: it accepts any Object, but will throw
@@ -463,7 +464,7 @@ object Byte extends AnyValCompanion {
463464
* @throws ClassCastException if the argument is not a java.lang.Byte
464465
* @return the Byte resulting from calling byteValue() on `x`
465466
*/
466-
def unbox(x: java.lang.Object): Byte = x.asInstanceOf[java.lang.Byte].byteValue()
467+
def unbox(x: java.lang.Object): Byte = ???
467468

468469
/** The String representation of the scala.Byte companion object. */
469470
override def toString = "object scala.Byte"

src/library/scala/Char.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ final abstract class Char private extends AnyVal {
434434
/** Returns the remainder of the division of this value by `x`. */
435435
def %(x: Double): Double
436436

437-
override def getClass(): Class[Char] = null
437+
// Provide a more specific return type for Scaladoc
438+
override def getClass(): Class[Char] = ???
438439
}
439440

440441
object Char extends AnyValCompanion {
@@ -451,7 +452,7 @@ object Char extends AnyValCompanion {
451452
* @param x the Char to be boxed
452453
* @return a java.lang.Character offering `x` as its underlying value.
453454
*/
454-
def box(x: Char): java.lang.Character = java.lang.Character.valueOf(x)
455+
def box(x: Char): java.lang.Character = ???
455456

456457
/** Transform a boxed type into a value type. Note that this
457458
* method is not typesafe: it accepts any Object, but will throw
@@ -463,7 +464,7 @@ object Char extends AnyValCompanion {
463464
* @throws ClassCastException if the argument is not a java.lang.Character
464465
* @return the Char resulting from calling charValue() on `x`
465466
*/
466-
def unbox(x: java.lang.Object): Char = x.asInstanceOf[java.lang.Character].charValue()
467+
def unbox(x: java.lang.Object): Char = ???
467468

468469
/** The String representation of the scala.Char companion object. */
469470
override def toString = "object scala.Char"

src/library/scala/Double.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ final abstract class Double private extends AnyVal {
200200
/** Returns the remainder of the division of this value by `x`. */
201201
def %(x: Double): Double
202202

203-
override def getClass(): Class[Double] = null
203+
// Provide a more specific return type for Scaladoc
204+
override def getClass(): Class[Double] = ???
204205
}
205206

206207
object Double extends AnyValCompanion {
@@ -229,7 +230,7 @@ object Double extends AnyValCompanion {
229230
* @param x the Double to be boxed
230231
* @return a java.lang.Double offering `x` as its underlying value.
231232
*/
232-
def box(x: Double): java.lang.Double = java.lang.Double.valueOf(x)
233+
def box(x: Double): java.lang.Double = ???
233234

234235
/** Transform a boxed type into a value type. Note that this
235236
* method is not typesafe: it accepts any Object, but will throw
@@ -241,7 +242,7 @@ object Double extends AnyValCompanion {
241242
* @throws ClassCastException if the argument is not a java.lang.Double
242243
* @return the Double resulting from calling doubleValue() on `x`
243244
*/
244-
def unbox(x: java.lang.Object): Double = x.asInstanceOf[java.lang.Double].doubleValue()
245+
def unbox(x: java.lang.Object): Double = ???
245246

246247
/** The String representation of the scala.Double companion object. */
247248
override def toString = "object scala.Double"

src/library/scala/Float.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ final abstract class Float private extends AnyVal {
200200
/** Returns the remainder of the division of this value by `x`. */
201201
def %(x: Double): Double
202202

203-
override def getClass(): Class[Float] = null
203+
// Provide a more specific return type for Scaladoc
204+
override def getClass(): Class[Float] = ???
204205
}
205206

206207
object Float extends AnyValCompanion {
@@ -229,7 +230,7 @@ object Float extends AnyValCompanion {
229230
* @param x the Float to be boxed
230231
* @return a java.lang.Float offering `x` as its underlying value.
231232
*/
232-
def box(x: Float): java.lang.Float = java.lang.Float.valueOf(x)
233+
def box(x: Float): java.lang.Float = ???
233234

234235
/** Transform a boxed type into a value type. Note that this
235236
* method is not typesafe: it accepts any Object, but will throw
@@ -241,7 +242,7 @@ object Float extends AnyValCompanion {
241242
* @throws ClassCastException if the argument is not a java.lang.Float
242243
* @return the Float resulting from calling floatValue() on `x`
243244
*/
244-
def unbox(x: java.lang.Object): Float = x.asInstanceOf[java.lang.Float].floatValue()
245+
def unbox(x: java.lang.Object): Float = ???
245246

246247
/** The String representation of the scala.Float companion object. */
247248
override def toString = "object scala.Float"

src/library/scala/Int.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ final abstract class Int private extends AnyVal {
434434
/** Returns the remainder of the division of this value by `x`. */
435435
def %(x: Double): Double
436436

437-
override def getClass(): Class[Int] = null
437+
// Provide a more specific return type for Scaladoc
438+
override def getClass(): Class[Int] = ???
438439
}
439440

440441
object Int extends AnyValCompanion {
@@ -451,7 +452,7 @@ object Int extends AnyValCompanion {
451452
* @param x the Int to be boxed
452453
* @return a java.lang.Integer offering `x` as its underlying value.
453454
*/
454-
def box(x: Int): java.lang.Integer = java.lang.Integer.valueOf(x)
455+
def box(x: Int): java.lang.Integer = ???
455456

456457
/** Transform a boxed type into a value type. Note that this
457458
* method is not typesafe: it accepts any Object, but will throw
@@ -463,7 +464,7 @@ object Int extends AnyValCompanion {
463464
* @throws ClassCastException if the argument is not a java.lang.Integer
464465
* @return the Int resulting from calling intValue() on `x`
465466
*/
466-
def unbox(x: java.lang.Object): Int = x.asInstanceOf[java.lang.Integer].intValue()
467+
def unbox(x: java.lang.Object): Int = ???
467468

468469
/** The String representation of the scala.Int companion object. */
469470
override def toString = "object scala.Int"

src/library/scala/Long.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ final abstract class Long private extends AnyVal {
434434
/** Returns the remainder of the division of this value by `x`. */
435435
def %(x: Double): Double
436436

437-
override def getClass(): Class[Long] = null
437+
// Provide a more specific return type for Scaladoc
438+
override def getClass(): Class[Long] = ???
438439
}
439440

440441
object Long extends AnyValCompanion {
@@ -451,7 +452,7 @@ object Long extends AnyValCompanion {
451452
* @param x the Long to be boxed
452453
* @return a java.lang.Long offering `x` as its underlying value.
453454
*/
454-
def box(x: Long): java.lang.Long = java.lang.Long.valueOf(x)
455+
def box(x: Long): java.lang.Long = ???
455456

456457
/** Transform a boxed type into a value type. Note that this
457458
* method is not typesafe: it accepts any Object, but will throw
@@ -463,7 +464,7 @@ object Long extends AnyValCompanion {
463464
* @throws ClassCastException if the argument is not a java.lang.Long
464465
* @return the Long resulting from calling longValue() on `x`
465466
*/
466-
def unbox(x: java.lang.Object): Long = x.asInstanceOf[java.lang.Long].longValue()
467+
def unbox(x: java.lang.Object): Long = ???
467468

468469
/** The String representation of the scala.Long companion object. */
469470
override def toString = "object scala.Long"

src/library/scala/Short.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ final abstract class Short private extends AnyVal {
434434
/** Returns the remainder of the division of this value by `x`. */
435435
def %(x: Double): Double
436436

437-
override def getClass(): Class[Short] = null
437+
// Provide a more specific return type for Scaladoc
438+
override def getClass(): Class[Short] = ???
438439
}
439440

440441
object Short extends AnyValCompanion {
@@ -451,7 +452,7 @@ object Short extends AnyValCompanion {
451452
* @param x the Short to be boxed
452453
* @return a java.lang.Short offering `x` as its underlying value.
453454
*/
454-
def box(x: Short): java.lang.Short = java.lang.Short.valueOf(x)
455+
def box(x: Short): java.lang.Short = ???
455456

456457
/** Transform a boxed type into a value type. Note that this
457458
* method is not typesafe: it accepts any Object, but will throw
@@ -463,7 +464,7 @@ object Short extends AnyValCompanion {
463464
* @throws ClassCastException if the argument is not a java.lang.Short
464465
* @return the Short resulting from calling shortValue() on `x`
465466
*/
466-
def unbox(x: java.lang.Object): Short = x.asInstanceOf[java.lang.Short].shortValue()
467+
def unbox(x: java.lang.Object): Short = ???
467468

468469
/** The String representation of the scala.Short companion object. */
469470
override def toString = "object scala.Short"

src/library/scala/Unit.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ package scala
1919
* method which is declared `void`.
2020
*/
2121
final abstract class Unit private extends AnyVal {
22-
override def getClass(): Class[Unit] = null
22+
// Provide a more specific return type for Scaladoc
23+
override def getClass(): Class[Unit] = ???
2324
}
2425

2526
object Unit extends AnyValCompanion {
@@ -29,7 +30,7 @@ object Unit extends AnyValCompanion {
2930
* @param x the Unit to be boxed
3031
* @return a scala.runtime.BoxedUnit offering `x` as its underlying value.
3132
*/
32-
def box(x: Unit): scala.runtime.BoxedUnit = scala.runtime.BoxedUnit.UNIT
33+
def box(x: Unit): scala.runtime.BoxedUnit = ???
3334

3435
/** Transform a boxed type into a value type. Note that this
3536
* method is not typesafe: it accepts any Object, but will throw
@@ -39,7 +40,7 @@ object Unit extends AnyValCompanion {
3940
* @throws ClassCastException if the argument is not a scala.runtime.BoxedUnit
4041
* @return the Unit value ()
4142
*/
42-
def unbox(x: java.lang.Object): Unit = ()
43+
def unbox(x: java.lang.Object): Unit = ???
4344

4445
/** The String representation of the scala.Unit companion object. */
4546
override def toString = "object scala.Unit"

0 commit comments

Comments
 (0)