Skip to content

Commit fc1622d

Browse files
committed
doc(union): typos, example using member of join
1 parent 87e713e commit fc1622d

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

docs/docs/reference/new-types/union-types-spec.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ class A extends C[A] with D
7676
class B extends C[B] with D with E
7777
```
7878

79-
The join of `A | B` is `C[A | B] with D`
79+
The join of `A | B` is `C[A | B] & D`
8080

8181
## Type inference
8282

83-
When inferring the result type of a definition (`val`, `var`, or `def`), if the
84-
type we are about to infer is a union type, we replace it by its join.
83+
When inferring the result type of a definition (`val`, `var`, or `def`) and the
84+
type we are about to infer is a union type, then we replace it by its join.
8585
Similarly, when instantiating a type argument, if the corresponding type
8686
parameter is not upper-bounded by a union type and the type we are about to
8787
instantiate is a union type, we replace it by its join. This mirrors the
8888
treatment of singleton types which are also widened to their underlying type
89-
unless explicitly specified. and the motivation is the same: inferring types
89+
unless explicitly specified. The motivation is the same: inferring types
9090
which are "too precise" can lead to unintuitive typechecking issues later on.
9191

9292
Note: Since this behavior limits the usability of union types, it might
@@ -127,6 +127,14 @@ trait B { def hello: String }
127127
def test(x: A | B) = x.hello // error: value `hello` is not a member of A | B
128128
```
129129

130+
On the otherhand, the following would be allowed
131+
```scala
132+
trait C { def hello: String }
133+
trait A extends C with D
134+
trait B extends C with E
135+
136+
def test(x: A | B) = x.hello // ok as `hello` is a member of the join of A | B which is C
137+
130138
## Exhaustivity checking
131139

132140
If the selector of a pattern match is a union type, the match is considered

0 commit comments

Comments
 (0)