Skip to content
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
}