Skip to content

Commit f745d16

Browse files
mghildiyjvican
authored andcommitted
Rewrote compound types tour #742 (#988)
1 parent 0cd4c9c commit f745d16

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

_tour/abstract-types.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ title: Abstract Types
44

55
discourse: true
66

7-
partof: scala-tour
8-
7+
tutorial: scala-tour
8+
categories: tour
99
num: 23
1010
next-page: compound-types
1111
previous-page: inner-classes
1212
prerequisite-knowledge: variance, upper-type-bound
1313

14-
redirect_from: "/tutorials/tour/abstract-types.html"
1514
---
1615

1716
Traits and abstract classes can have an abstract type member. This means that the concrete implementations define the actual type. Here's an example:

_tour/self-types.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ title: Self-type
44

55
discourse: true
66

7-
partof: scala-tour
8-
7+
tutorial: scala-tour
8+
categories: tour
99
num: 25
1010
next-page: implicit-parameters
1111
previous-page: compound-types
1212
prerequisite-knowledge: nested-classes, mixin-class-composition
13-
14-
redirect_from: "/tutorials/tour/self-types.html"
1513
---
1614
Self-types are a way to declare that a trait must be mixed into another trait, even though it doesn't directly extend it. That makes the members of the dependency available without imports.
1715

@@ -29,7 +27,7 @@ trait Tweeter {
2927
}
3028
3129
class VerifiedTweeter(val username_ : String) extends Tweeter with User { // We mixin User because Tweeter required it
32-
def username = s"real $username_"
30+
def username = s"real $username_"
3331
}
3432
3533
val realBeyoncé = new VerifiedTweeter("Beyoncé")

_tour/traits.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ title: Traits
44

55
discourse: true
66

7-
partof: scala-tour
8-
7+
tutorial: scala-tour
8+
categories: tour
99
num: 5
1010
next-page: mixin-class-composition
1111
previous-page: classes
1212
assumed-knowledge: expressions, classes, generics, objects, companion-objects
13-
14-
redirect_from: "/tutorials/tour/traits.html"
1513
---
1614

1715
Traits are used to share interfaces and fields between classes. They are similar to Java 8's interfaces. Classes and objects can extend traits but traits cannot be instantiated and therefore have no parameters.
@@ -36,6 +34,11 @@ Extending the `trait Iterator[A]` requires a type `A` and implementations of the
3634
## Using traits
3735
Use the `extends` keyword to extend a trait. Then implement any abstract members of the trait using the `override` keyword:
3836
```tut
37+
trait Iterator[A] {
38+
def hasNext: Boolean
39+
def next(): A
40+
}
41+
3942
class IntIterator(to: Int) extends Iterator[Int] {
4043
private var current = 0
4144
override def hasNext: Boolean = current < to
@@ -50,13 +53,13 @@ class IntIterator(to: Int) extends Iterator[Int] {
5053
5154
5255
val iterator = new IntIterator(10)
53-
println(iterator.next()) // prints 0
54-
println(iterator.next()) // prints 1
56+
iterator.next() // returns 0
57+
iterator.next() // returns 1
5558
```
5659
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.
5760

5861
## Subtyping
59-
Subtypes of traits can be used where the trait is required.
62+
Where a given trait is required, a subtype of the trait can be used instead.
6063
```tut
6164
import scala.collection.mutable.ArrayBuffer
6265

0 commit comments

Comments
 (0)