Skip to content

Commit 454ae7e

Browse files
committed
bias towards intersection types
1 parent 28028df commit 454ae7e

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

_tour/compound-types.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: tour
3-
title: Compound Types
3+
title: Intersection Types, aka Compound Types
44
partof: scala-tour
55

66
num: 26
@@ -10,15 +10,18 @@ previous-page: abstract-type-members
1010
redirect_from: "/tutorials/tour/compound-types.html"
1111
---
1212

13-
Sometimes it is necessary to express that the type of an object is a subtype of several other types. In Scala this can be expressed with the help of *compound types*, which are intersections of object types.
13+
Sometimes it is necessary to express that the type of an object is a subtype of several other types.
14+
15+
In Scala this can be expressed with the help of *intersection types*, (or *compound types* in
16+
Scala 2) which are types that behave like any part of the intersection.
1417

1518
Suppose we have two traits `Cloneable` and `Resetable`:
1619

1720
{% tabs compound-types_1 class=tabs-scala-version %}
1821
{% tab 'Scala 2' for=compound-types_1 %}
1922
```scala mdoc
2023
trait Cloneable extends java.lang.Cloneable {
21-
override def clone(): Cloneable = {
24+
override def clone(): Cloneable = { // makes clone public
2225
super.clone().asInstanceOf[Cloneable]
2326
}
2427
}
@@ -30,7 +33,7 @@ trait Resetable {
3033
{% tab 'Scala 3' for=compound-types_1 %}
3134
```scala
3235
trait Cloneable extends java.lang.Cloneable:
33-
override def clone(): Cloneable =
36+
override def clone(): Cloneable = // makes clone public
3437
super.clone().asInstanceOf[Cloneable]
3538
trait Resetable:
3639
def reset: Unit
@@ -63,29 +66,28 @@ def cloneAndReset(obj: ?): Cloneable =
6366
The question arises what the type of the parameter `obj` is. If it's `Cloneable` then the object can be `clone`d, but not `reset`; if it's `Resetable` we can `reset` it, but there is no `clone` operation. To avoid type casts in such a situation, we can specify the type of `obj` to be both `Cloneable` and `Resetable`.
6467
{% tabs compound-types_3 class=tabs-scala-version %}
6568
{% tab 'Scala 2' for=compound-types_3 %}
66-
This compound type is written like this in Scala: `Cloneable with Resetable`.
69+
This compound type is written in Scala as `Cloneable with Resetable`.
6770

6871
Here's the updated function:
6972
```scala mdoc:fail
7073
def cloneAndReset(obj: Cloneable with Resetable): Cloneable = {
7174
//...
7275
}
7376
```
74-
Compound types can consist of several object types and they may have a single refinement which can be used to narrow the signature of existing object members.
7577
Note that you can have more than two types: `A with B with C with ...`.
76-
This means the same as thing as `(...(A with B) with C`) with ... )`
78+
This means the same as thing as `(...(A with B) with C) with ... )`
7779
{% endtab %}
7880
{% tab 'Scala 3' for=compound-types_3 %}
79-
This compound type is written like this in Scala: `Cloneable & Resetable`.
81+
This intersection type is written in Scala as `Cloneable & Resetable`.
8082

8183
Here's the updated function:
8284
```scala
8385
def cloneAndReset(obj: Cloneable & Resetable): Cloneable = {
8486
//...
8587
}
8688
```
87-
Compound types can consist of several object types and they may have a single refinement which can be used to narrow the signature of existing object members.
88-
Note that you can have more than two types: `A & B & C & ...`.
89+
<!-- Compound types can consist of several object types and they may have a single refinement which can be used to narrow the signature of existing object members. -->
90+
Note that you can have more than two types: `A & B & C & ...`.
8991
And `&` is associative, so parentheses can be added around any part without changing the meaning.
9092
{% endtab %}
9193
{% endtabs %}

0 commit comments

Comments
 (0)