Skip to content

Commit 94a0d4b

Browse files
committed
Moved compound types section to traits section
1 parent c5e811c commit 94a0d4b

File tree

4 files changed

+38
-54
lines changed

4 files changed

+38
-54
lines changed

tutorials/tour/_posts/2017-02-13-abstract-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ disqus: true
77
tutorial: scala-tour
88
categories: tour
99
num: 23
10-
next-page: compound-types
10+
next-page: self-types
1111
previous-page: inner-classes
1212
prerequisite-knowledge: variance, upper-type-bound
1313
---

tutorials/tour/_posts/2017-02-13-compound-types.md

Lines changed: 0 additions & 52 deletions
This file was deleted.

tutorials/tour/_posts/2017-02-13-self-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tutorial: scala-tour
88
categories: tour
99
num: 25
1010
next-page: implicit-parameters
11-
previous-page: compound-types
11+
previous-page: abstract-types
1212
prerequisite-knowledge: nested-classes, mixin-class-composition
1313
---
1414
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.

tutorials/tour/_posts/2017-02-13-traits.md

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

2424
Traits become especially useful as generic types and with abstract methods.
25+
2526
```tut
2627
trait Iterator[A] {
2728
def hasNext: Boolean
@@ -33,6 +34,7 @@ Extending the `trait Iterator[A]` requires a type `A` and implementations of the
3334

3435
## Using traits
3536
Use the `extends` keyword to extend a trait. Then implement any abstract members of the trait using the `override` keyword:
37+
3638
```tut
3739
trait Iterator[A] {
3840
def hasNext: Boolean
@@ -56,11 +58,13 @@ class IntIterator(to: Int) extends Iterator[Int] {
5658
val iterator = new IntIterator(10)
5759
iterator.next() // prints 0
5860
iterator.next() // prints 1
61+
5962
```
6063
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.
6164

6265
## Subtyping
6366
Subtypes of traits can be used where a the trait is required.
67+
6468
```tut
6569
import scala.collection.mutable.ArrayBuffer
6670
@@ -79,4 +83,36 @@ animals.append(dog)
7983
animals.append(cat)
8084
animals.foreach(pet => println(pet.name)) // Prints Harry Sally
8185
```
86+
8287
The `trait Pet` has an abstract field `name` which 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`.
88+
89+
## Multiple Parameter Types
90+
91+
Sometimes it is necessary to have parameter which has multiple supertypes. In Scala you can use the `with` keyword to express the intersection of multiple traits.
92+
93+
Suppose we have two traits `Cloneable` and `Resetable`:
94+
95+
```tut
96+
trait Cloneable extends java.lang.Cloneable {
97+
override def clone(): Cloneable = {
98+
super.clone().asInstanceOf[Cloneable]
99+
}
100+
}
101+
trait Resetable {
102+
def reset: Unit
103+
}
104+
```
105+
106+
Now suppose we want to write a function `cloneAndReset` which takes an object, clones it and resets the original object. We use the keyword `with` to specify that the object must be a subtype of both types.
107+
108+
```
109+
def cloneAndReset(obj: Cloneable with Resetable): Cloneable = {
110+
val cloned = obj.clone()
111+
obj.reset
112+
cloned
113+
}
114+
```
115+
116+
Because the type of `obj` is `Cloneable with Resetable`, we know the object has both a `clone` and a `reset` method.
117+
118+
Compound types can consist of several object types. The general form is: `A with B with C ...`.

0 commit comments

Comments
 (0)