Skip to content

Commit 470b360

Browse files
committed
Tabs for Scala 2/3 in tour/traits.
1 parent 27947ff commit 470b360

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

_tour/traits.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,36 @@ trait HairColor
2222
```
2323

2424
Traits become especially useful as generic types and with abstract methods.
25+
26+
{% tabs trait-iterator-definition class=tabs-scala-version %}
27+
28+
{% tab 'Scala 2' for=trait-iterator-definition %}
2529
```scala mdoc
2630
trait Iterator[A] {
2731
def hasNext: Boolean
2832
def next(): A
2933
}
3034
```
35+
{% endtab %}
36+
37+
{% tab 'Scala 3' for=trait-iterator-definition %}
38+
```scala
39+
trait Iterator[A]:
40+
def hasNext: Boolean
41+
def next(): A
42+
```
43+
{% endtab %}
44+
45+
{% endtabs %}
3146

3247
Extending the `trait Iterator[A]` requires a type `A` and implementations of the methods `hasNext` and `next`.
3348

3449
## Using traits
3550
Use the `extends` keyword to extend a trait. Then implement any abstract members of the trait using the `override` keyword:
51+
52+
{% tabs trait-intiterator-definition class=tabs-scala-version %}
53+
54+
{% tab 'Scala 2' for=trait-intiterator-definition %}
3655
```scala mdoc:nest
3756
trait Iterator[A] {
3857
def hasNext: Boolean
@@ -51,15 +70,46 @@ class IntIterator(to: Int) extends Iterator[Int] {
5170
}
5271
}
5372

73+
val iterator = new IntIterator(10)
74+
iterator.next() // returns 0
75+
iterator.next() // returns 1
76+
```
77+
{% endtab %}
78+
79+
{% tab 'Scala 3' for=trait-intiterator-definition %}
80+
```scala
81+
trait Iterator[A]:
82+
def hasNext: Boolean
83+
def next(): A
84+
85+
class IntIterator(to: Int) extends Iterator[Int]:
86+
private var current = 0
87+
override def hasNext: Boolean = current < to
88+
override def next(): Int =
89+
if hasNext then
90+
val t = current
91+
current += 1
92+
t
93+
else
94+
0
95+
end IntIterator
5496

5597
val iterator = new IntIterator(10)
5698
iterator.next() // returns 0
5799
iterator.next() // returns 1
58100
```
101+
{% endtab %}
102+
103+
{% endtabs %}
104+
59105
This `IntIterator` class takes a parameter `to` as an upper bound. It `extends Iterator[Int]` which means that the `next` method must return an Int.
60106

61107
## Subtyping
62108
Where a given trait is required, a subtype of the trait can be used instead.
109+
110+
{% tabs trait-pet-example class=tabs-scala-version %}
111+
112+
{% tab 'Scala 2' for=trait-pet-example %}
63113
```scala mdoc
64114
import scala.collection.mutable.ArrayBuffer
65115

@@ -78,6 +128,30 @@ animals.append(dog)
78128
animals.append(cat)
79129
animals.foreach(pet => println(pet.name)) // Prints Harry Sally
80130
```
131+
{% endtab %}
132+
133+
{% tab 'Scala 3' for=trait-pet-example %}
134+
```scala
135+
import scala.collection.mutable.ArrayBuffer
136+
137+
trait Pet:
138+
val name: String
139+
140+
class Cat(val name: String) extends Pet
141+
class Dog(val name: String) extends Pet
142+
143+
val dog = Dog("Harry")
144+
val cat = Cat("Sally")
145+
146+
val animals = ArrayBuffer.empty[Pet]
147+
animals.append(dog)
148+
animals.append(cat)
149+
animals.foreach(pet => println(pet.name)) // Prints Harry Sally
150+
```
151+
{% endtab %}
152+
153+
{% endtabs %}
154+
81155
The `trait Pet` has an abstract field `name` that gets implemented by Cat and Dog in their constructors. On the last line, we call `pet.name`, which must be implemented in any subtype of the trait `Pet`.
82156

83157

0 commit comments

Comments
 (0)