Skip to content

Tweak literal types spec part about asInstanceOf #1418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions _sips/sips/2014-06-27-42.type.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -104,16 +105,14 @@ 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 {
case one: 1 => true
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
Expand Down Expand Up @@ -519,17 +518,17 @@ 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
Patterns](http://scala-lang.org/files/archive/spec/2.12/08-pattern-matching.html#typed-patterns))
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,
```
Expand All @@ -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`,
Expand Down