Skip to content

Clarifications in the docs on intersection types #5207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/docs/reference/intersection-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
19 changes: 19 additions & 0 deletions tests/pos/intersection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}