File tree Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Original file line number Diff line number Diff line change @@ -20,16 +20,16 @@ def f(x: Resettable & Growable[String]) = {
2020
2121The 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
2926The members of an intersection type ` A & B ` are all the members of ` A `
3027and all the members of ` B ` . For instance ` Resettable & Growable[String] `
3128has 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+
3333If a member appears in both ` A ` and ` B ` , its type in ` A & B ` is the
3434intersection of its type in ` A ` and its type in ` B ` . For instance, assume the definitions:
3535
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments