From 4f0e54b1000e155cdd8d1ba3e6de7d7bd02eb933 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 4 Jul 2019 18:16:59 +0100 Subject: [PATCH] Tweak literal types spec part about asInstanceOf --- _sips/sips/2014-06-27-42.type.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/_sips/sips/2014-06-27-42.type.md b/_sips/sips/2014-06-27-42.type.md index afe0aeaeae..701c01c43c 100644 --- a/_sips/sips/2014-06-27-42.type.md +++ b/_sips/sips/2014-06-27-42.type.md @@ -23,6 +23,7 @@ Sabin** | Feb 9th 2017 | New author volunteered to update the SIP for review | | Nov 16th 2017 | Updated following implementation experience in Typelevel Scala | | Aug 7th 2018 | Updated to remove Symbol literals, which won't be supported in 3 | +| Jul 4th 2019 | Drop behaviour spec around asInstanceOf, which is a user assertion | ## Introduction @@ -104,7 +105,7 @@ Lightbend Scala compiler. narrow(23) // result is 23: 23 ``` -+ Pattern matching against literal types and `isInstanceOf`/`asInstanceOf` tests/conversions are ++ Pattern matching against literal types and `isInstanceOf` tests are implemented via equality/identity tests of the corresponding values. ``` (1: Any) match { @@ -112,8 +113,6 @@ Lightbend Scala compiler. case _ => false } // result is true (1: Any).isInstanceOf[1] // result is true - (1: Any).asInstanceOf[1] // result is 1: 1 - (1: Any).asInstanceOf[2] // ClassCastException ``` + A `scala.ValueOf[T]` type class and corresponding `scala.Predef.valueOf[T]` operator has been @@ -519,7 +518,7 @@ applications which work with large datasets. valueOf[23] // result is 23: 23 ``` -+ Pattern matching against literal types and `isInstanceOf`/`asInstanceOf` tests/conversions are ++ Pattern matching against literal types and `isInstanceOf` tests are implemented via equality/identity tests of the corresponding values. Pattern matching against typed patterns (see [8.1.2 Typed @@ -527,9 +526,9 @@ applications which work with large datasets. where the `TypePat` is a literal type is translated as a match against the subsuming non-singleton type followed by an equality test with the value corresponding to the literal type. - Where applied to literal types `isInstanceOf` and `asInstanceOf` are translated to a test against + Where applied to literal types `isInstanceOf` is translated to a test against the subsuming non-singleton type and an equality test with the value corresponding to the literal - type. In the case of `asInstanceOf` a `ClassCastException` is thrown if the test fails. + type. Examples, ``` @@ -538,10 +537,13 @@ applications which work with large datasets. case _ => false } // result is true (1: Any).isInstanceOf[1] // result is true - (1: Any).asInstanceOf[1] // result is 1: 1 - (1: Any).asInstanceOf[2] // ClassCastException ``` + Importantly, that doesn't include `asInstanceOf` as that is a user assertion to the compiler, with + the compiler inserting in the generated code just enough code for the underlying runtime to not + give a `ValidationError`. The compiler should not, for instance, generate code such that an + expression like `(1: Any).asInstanceOf[2]` would throw a `ClassCastException`. + + Default initialization for vars with literal types is forbidden. The default initializer for a var is already mandated to be it's natural zero element (`0`,