From c1c811f34e586e0dc116fba670e093307964c679 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 5 Oct 2018 09:08:12 +0200 Subject: [PATCH] Clarifications in the docs on intersection types --- docs/docs/reference/intersection-types.md | 10 +++++----- tests/pos/intersection.scala | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/docs/docs/reference/intersection-types.md b/docs/docs/reference/intersection-types.md index 1335633d8538..3e3ec3d9941b 100644 --- a/docs/docs/reference/intersection-types.md +++ b/docs/docs/reference/intersection-types.md @@ -20,16 +20,16 @@ def f(x: Resettable & Growable[String]) = { The value `x` is required to be _both_ a `Resettable` and a `Growable[String]`. Intersection types `A & B` replace compound types -`A with B` in Scala 2 (for the moment, `A with B` is still allowed, but -it will be deprecated and removed in the future). - -Unlike `with` types, `&` is _commutative_: `A & B` is the same type as -`B & A`. +`A with B` in Scala 2. For the moment, `A with B` is still allowed, but +its usage as a type (as opposed to in a `new` or `extends` clause) will be deprecated and removed in the future. The members of an intersection type `A & B` are all the members of `A` and all the members of `B`. For instance `Resettable & Growable[String]` has member methods `reset` and `add`. +`&` is _commutative_: `A & B` is the same type as `B & A`, in that sense that the two types +have the same values and are subtypes of each other. + If a member appears in both `A` and `B`, its type in `A & B` is the intersection of its type in `A` and its type in `B`. For instance, assume the definitions: diff --git a/tests/pos/intersection.scala b/tests/pos/intersection.scala index faf9af401d58..094e6a4e9e8e 100644 --- a/tests/pos/intersection.scala +++ b/tests/pos/intersection.scala @@ -22,3 +22,22 @@ object intersection { def g: C[A | B] = f def h: C[A] & C[B] = g } +object Test { + + trait A { + def f: Any + } + trait B extends A { + override def f: Int = 1 + } + trait C extends A { + def f: Any = "" + } + + val bc: B with C = new C with B {} + + def fooAB = (??? : A with B).f + def fooAB1: Int = fooAB + def fooBA = (??? : B with A).f + def fooBA1: Int = fooBA +} \ No newline at end of file