Skip to content

Fix #8175: Explain transparency of toplevel opaque types #8201

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
Feb 6, 2020
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions docs/docs/reference/other-new-features/opaques-details.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ object o {
def id(x: o.T): o.T = x
```

### Toplevel Opaque Types

An opaque type on the toplevel is transparent in all other toplevel definitions in the sourcefile where it appears, but is opaque in nested
objects and classes and in all other source files. Example:
```scala
// in test1.scala
opaque type A = String
val x: A = "abc"

object obj {
val y: A = "abc" // error: found: "abc", required: A
}

// in test2.scala
def z: String = x // error: found: A, required: String
```
This behavior becomes clear if one recalls that toplevel definitions are placed in their own synthetic object. For instance, the code in `test1.scala` would expand to
```scala
object test1$package {
opaque type A = String
val x: A = "abc"
}
object obj {
val y: A = "abc" // error: cannot assign "abc" to opaque type A
}
```
The opaque type `A` is transparent in its scope, which includes the definition of `x`, but not the definitions of `obj` and `y`.


### Relationship to SIP 35

Opaque types in Dotty are an evolution from what is described in
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/i8175.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
opaque type A = String
val x: A = "abc"

object obj {
val y: A = "abc" // error: cannot assign "abc" to opaque type A
}