Skip to content

Commit 781cc24

Browse files
committed
Add code tabs for _tour/compound-types
1 parent de06e87 commit 781cc24

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

_tour/compound-types.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Sometimes it is necessary to express that the type of an object is a subtype of
1414

1515
Suppose we have two traits `Cloneable` and `Resetable`:
1616

17+
{% tabs compound-types_1 class=tabs-scala-version %}
18+
{% tab 'Scala 2' for=compound-types_1 %}
1719
```scala mdoc
1820
trait Cloneable extends java.lang.Cloneable {
1921
override def clone(): Cloneable = {
@@ -24,28 +26,66 @@ trait Resetable {
2426
def reset: Unit
2527
}
2628
```
29+
{% endtab %}
30+
{% tab 'Scala 3' for=compound-types_1 %}
31+
```scala
32+
trait Cloneable extends java.lang.Cloneable:
33+
override def clone(): Cloneable =
34+
super.clone().asInstanceOf[Cloneable]
35+
trait Resetable:
36+
def reset: Unit
37+
```
38+
{% endtab %}
39+
{% endtabs %}
2740

2841
Now suppose we want to write a function `cloneAndReset` which takes an object, clones it and resets the original object:
2942

30-
```
43+
{% tabs compound-types_2 class=tabs-scala-version %}
44+
{% tab 'Scala 2' for=compound-types_2 %}
45+
```scala mdoc::fail
3146
def cloneAndReset(obj: ?): Cloneable = {
3247
val cloned = obj.clone()
3348
obj.reset
3449
cloned
3550
}
3651
```
52+
{% endtab %}
53+
{% tab 'Scala 3' for=compound-types_2 %}
54+
```scala
55+
def cloneAndReset(obj: ?): Cloneable =
56+
val cloned = obj.clone()
57+
obj.reset
58+
cloned
59+
```
60+
{% endtab %}
61+
{% endtabs %}
3762

38-
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`. This compound type is written like this in Scala: `Cloneable with Resetable`.
63+
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`.
64+
{% tabs compound-types_3 class=tabs-scala-version %}
65+
{% tab 'Scala 2' for=compound-types_3 %}
66+
This compound type is written like this in Scala: `Cloneable with Resetable`.
3967

4068
Here's the updated function:
41-
42-
```
69+
```scala mdoc
4370
def cloneAndReset(obj: Cloneable with Resetable): Cloneable = {
4471
//...
4572
}
4673
```
47-
4874
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.
4975
The general form is: `A with B with C ... { refinement }`
76+
{% endtab %}
77+
{% tab 'Scala 3' for=compound-types_2 %}
78+
This compound type is written like this in Scala: `Cloneable & Resetable`.
79+
80+
Here's the updated function:
81+
```scala
82+
def cloneAndReset(obj: Cloneable & Resetable): Cloneable = {
83+
//...
84+
}
85+
```
86+
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.
87+
The general form is: `A & B & C ... { refinement }`
88+
{% endtab %}
89+
{% endtabs %}
5090

5191
An example for the use of refinements is given on the page about [class composition with mixins](mixin-class-composition.html).

0 commit comments

Comments
 (0)