From 0ec677afbcc010e19a6284907e8cb619503bc565 Mon Sep 17 00:00:00 2001 From: Ghildiyal Date: Sun, 14 Jan 2018 00:26:08 +0530 Subject: [PATCH] Rewrote compound types tour #742 --- _tour/abstract-types.md | 5 ++--- _tour/self-types.md | 8 +++----- _tour/traits.md | 17 ++++++++++------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/_tour/abstract-types.md b/_tour/abstract-types.md index 61b7651a4c..61de402795 100644 --- a/_tour/abstract-types.md +++ b/_tour/abstract-types.md @@ -4,14 +4,13 @@ title: Abstract Types discourse: true -partof: scala-tour - +tutorial: scala-tour +categories: tour num: 23 next-page: compound-types previous-page: inner-classes prerequisite-knowledge: variance, upper-type-bound -redirect_from: "/tutorials/tour/abstract-types.html" --- Traits and abstract classes can have an abstract type member. This means that the concrete implementations define the actual type. Here's an example: diff --git a/_tour/self-types.md b/_tour/self-types.md index fb357f7151..a2a8a93c6e 100644 --- a/_tour/self-types.md +++ b/_tour/self-types.md @@ -4,14 +4,12 @@ title: Self-type discourse: true -partof: scala-tour - +tutorial: scala-tour +categories: tour num: 25 next-page: implicit-parameters previous-page: compound-types prerequisite-knowledge: nested-classes, mixin-class-composition - -redirect_from: "/tutorials/tour/self-types.html" --- 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. @@ -29,7 +27,7 @@ trait Tweeter { } class VerifiedTweeter(val username_ : String) extends Tweeter with User { // We mixin User because Tweeter required it - def username = s"real $username_" + def username = s"real $username_" } val realBeyoncé = new VerifiedTweeter("Beyoncé") diff --git a/_tour/traits.md b/_tour/traits.md index 020513e334..282f3321eb 100644 --- a/_tour/traits.md +++ b/_tour/traits.md @@ -4,14 +4,12 @@ title: Traits discourse: true -partof: scala-tour - +tutorial: scala-tour +categories: tour num: 5 next-page: mixin-class-composition previous-page: classes assumed-knowledge: expressions, classes, generics, objects, companion-objects - -redirect_from: "/tutorials/tour/traits.html" --- 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 ## 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 + def next(): A +} + class IntIterator(to: Int) extends Iterator[Int] { private var current = 0 override def hasNext: Boolean = current < to @@ -50,13 +53,13 @@ class IntIterator(to: Int) extends Iterator[Int] { val iterator = new IntIterator(10) -println(iterator.next()) // prints 0 -println(iterator.next()) // prints 1 +iterator.next() // returns 0 +iterator.next() // returns 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 the trait is required. +Where a given trait is required, a subtype of the trait can be used instead. ```tut import scala.collection.mutable.ArrayBuffer