Skip to content

Rewrote compound types tour #742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tutorials/tour/_posts/2017-02-13-abstract-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ disqus: true
tutorial: scala-tour
categories: tour
num: 23
next-page: compound-types
next-page: self-types
previous-page: inner-classes
prerequisite-knowledge: variance, upper-type-bound
---
Expand Down
52 changes: 0 additions & 52 deletions tutorials/tour/_posts/2017-02-13-compound-types.md

This file was deleted.

2 changes: 1 addition & 1 deletion tutorials/tour/_posts/2017-02-13-self-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tutorial: scala-tour
categories: tour
num: 25
next-page: implicit-parameters
previous-page: compound-types
previous-page: abstract-types
prerequisite-knowledge: nested-classes, mixin-class-composition
---
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.
Expand Down
36 changes: 36 additions & 0 deletions tutorials/tour/_posts/2017-02-13-traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ trait HairColor
```

Traits become especially useful as generic types and with abstract methods.

```tut
trait Iterator[A] {
def hasNext: Boolean
Expand All @@ -33,6 +34,7 @@ Extending the `trait Iterator[A]` requires a type `A` and implementations of the

## Using traits
Use the `extends` keyword to extend a trait. Then implement any abstract members of the trait using the `override` keyword:

```tut
trait Iterator[A] {
def hasNext: Boolean
Expand All @@ -56,11 +58,13 @@ class IntIterator(to: Int) extends Iterator[Int] {
val iterator = new IntIterator(10)
iterator.next() // prints 0
iterator.next() // prints 1

```
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.

## Subtyping
Subtypes of traits can be used where a the trait is required.

```tut
import scala.collection.mutable.ArrayBuffer

Expand All @@ -79,4 +83,36 @@ animals.append(dog)
animals.append(cat)
animals.foreach(pet => println(pet.name)) // Prints Harry Sally
```

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`.

## Multiple Parameter Types

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.

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

```tut
trait Cloneable extends java.lang.Cloneable {
override def clone(): Cloneable = {
super.clone().asInstanceOf[Cloneable]
}
}
trait Resetable {
def reset: Unit
}
```

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.

```
def cloneAndReset(obj: Cloneable with Resetable): Cloneable = {
val cloned = obj.clone()
obj.reset
cloned
}
```

Because the type of `obj` is `Cloneable with Resetable`, we know the object has both a `clone` and a `reset` method.

Compound types can consist of several object types. The general form is: `A with B with C ...`.