Skip to content

Commit 1e86445

Browse files
authored
Merge pull request #5207 from dotty-staging/fix-intersection-docs
Clarifications in the docs on intersection types
2 parents 3f86587 + c1c811f commit 1e86445

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

docs/docs/reference/intersection-types.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ def f(x: Resettable & Growable[String]) = {
2020

2121
The value `x` is required to be _both_ a `Resettable` and a
2222
`Growable[String]`. Intersection types `A & B` replace compound types
23-
`A with B` in Scala 2 (for the moment, `A with B` is still allowed, but
24-
it will be deprecated and removed in the future).
25-
26-
Unlike `with` types, `&` is _commutative_: `A & B` is the same type as
27-
`B & A`.
23+
`A with B` in Scala 2. For the moment, `A with B` is still allowed, but
24+
its usage as a type (as opposed to in a `new` or `extends` clause) will be deprecated and removed in the future.
2825

2926
The members of an intersection type `A & B` are all the members of `A`
3027
and all the members of `B`. For instance `Resettable & Growable[String]`
3128
has member methods `reset` and `add`.
3229

30+
`&` is _commutative_: `A & B` is the same type as `B & A`, in that sense that the two types
31+
have the same values and are subtypes of each other.
32+
3333
If a member appears in both `A` and `B`, its type in `A & B` is the
3434
intersection of its type in `A` and its type in `B`. For instance, assume the definitions:
3535

tests/pos/intersection.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,22 @@ object intersection {
2222
def g: C[A | B] = f
2323
def h: C[A] & C[B] = g
2424
}
25+
object Test {
26+
27+
trait A {
28+
def f: Any
29+
}
30+
trait B extends A {
31+
override def f: Int = 1
32+
}
33+
trait C extends A {
34+
def f: Any = ""
35+
}
36+
37+
val bc: B with C = new C with B {}
38+
39+
def fooAB = (??? : A with B).f
40+
def fooAB1: Int = fooAB
41+
def fooBA = (??? : B with A).f
42+
def fooBA1: Int = fooBA
43+
}

0 commit comments

Comments
 (0)