Skip to content

Commit dcc7e8f

Browse files
authored
Merge pull request #8201 from dotty-staging/fix-#8175
Fix #8175: Explain transparency of toplevel opaque types
2 parents f5940fe + a48b13f commit dcc7e8f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

docs/docs/reference/other-new-features/opaques-details.md

+29
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,35 @@ object o {
4646
def id(x: o.T): o.T = x
4747
```
4848

49+
### Toplevel Opaque Types
50+
51+
An opaque type on the toplevel is transparent in all other toplevel definitions in the sourcefile where it appears, but is opaque in nested
52+
objects and classes and in all other source files. Example:
53+
```scala
54+
// in test1.scala
55+
opaque type A = String
56+
val x: A = "abc"
57+
58+
object obj {
59+
val y: A = "abc" // error: found: "abc", required: A
60+
}
61+
62+
// in test2.scala
63+
def z: String = x // error: found: A, required: String
64+
```
65+
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
66+
```scala
67+
object test1$package {
68+
opaque type A = String
69+
val x: A = "abc"
70+
}
71+
object obj {
72+
val y: A = "abc" // error: cannot assign "abc" to opaque type A
73+
}
74+
```
75+
The opaque type `A` is transparent in its scope, which includes the definition of `x`, but not the definitions of `obj` and `y`.
76+
77+
4978
### Relationship to SIP 35
5079

5180
Opaque types in Dotty are an evolution from what is described in

tests/neg/i8175.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
opaque type A = String
2+
val x: A = "abc"
3+
4+
object obj {
5+
val y: A = "abc" // error: cannot assign "abc" to opaque type A
6+
}

0 commit comments

Comments
 (0)