You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/other-new-features/opaques-details.md
+29
Original file line number
Diff line number
Diff line change
@@ -46,6 +46,35 @@ object o {
46
46
defid(x: o.T): o.T= x
47
47
```
48
48
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
+
opaquetypeA=String
56
+
valx:A="abc"
57
+
58
+
objectobj {
59
+
valy:A="abc"// error: found: "abc", required: A
60
+
}
61
+
62
+
// in test2.scala
63
+
defz: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
+
objecttest1$package {
68
+
opaquetypeA=String
69
+
valx:A="abc"
70
+
}
71
+
objectobj {
72
+
valy: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
+
49
78
### Relationship to SIP 35
50
79
51
80
Opaque types in Dotty are an evolution from what is described in
0 commit comments