ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 02.01.2026

Просмотров: 3445

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Section 12.1

Chapter 12 · Traits

260

class Animal

class Frog extends Animal with Philosophical { override def toString = "green"

}

Listing 12.3 · Mixing in a trait using with.

class Animal trait HasLegs

class Frog extends Animal with Philosophical with HasLegs { override def toString = "green"

}

Listing 12.4 · Mixing in multiple traits.

In the examples you’ve seen so far, class Frog has inherited an implementation of philosophize from trait Philosophical. Alternatively, Frog could override philosophize. The syntax looks the same as overriding a method declared in a superclass. Here’s an example:

class Animal

class Frog extends Animal with Philosophical { override def toString = "green"

override def philosophize() {

println("It ain't easy being "+ toString +"!")

}

}

Because this new definition of Frog still mixes in trait Philosophical, you can still use it from a variable of that type. But because Frog overrides Philosophical’s implementation of philosophize, you’ll get a new behavior when you call it:

scala> val phrog: Philosophical = new Frog phrog: Philosophical = green

scala> phrog.philosophize() It ain't easy being green!

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index

Section 12.2

Chapter 12 · Traits

261

At this point you might philosophize that traits are like Java interfaces with concrete methods, but they can actually do much more. Traits can, for example, declare fields and maintain state. In fact, you can do anything in a trait definition that you can do in a class definition, and the syntax looks exactly the same, with only two exceptions. First, a trait cannot have any “class” parameters, i.e., parameters passed to the primary constructor of a class. In other words, although you could define a class like this:

class Point(x: Int, y: Int)

The following attempt to define a trait would not compile:

trait NoPoint(x: Int, y: Int) // Does not compile

You’ll find out in Section 20.5 how to work around this restriction.

The other difference between classes and traits is that whereas in classes, super calls are statically bound, in traits, they are dynamically bound. If you write “super.toString” in a class, you know exactly which method implementation will be invoked. When you write the same thing in a trait, however, the method implementation to invoke for the super call is undefined when you define the trait. Rather, the implementation to invoke will be determined anew each time the trait is mixed into a concrete class. This curious behavior of super is key to allowing traits to work as stackable modifications, which will be described in Section 12.5. The rules for resolving super calls will be given in Section 12.6.

12.2 Thin versus rich interfaces

One major use of traits is to automatically add methods to a class in terms of methods the class already has. That is, traits can enrich a thin interface, making it into a rich interface.

Thin versus rich interfaces represents a commonly faced trade-off in object-oriented design. The trade-off is between the implementers and the clients of an interface. A rich interface has many methods, which make it convenient for the caller. Clients can pick a method that exactly matches the functionality they need. A thin interface, on the other hand, has fewer methods, and thus is easier on the implementers. Clients calling into a thin interface, however, have to write more code. Given the smaller selection of

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index



Section 12.3

Chapter 12 · Traits

262

methods to call, they may have to choose a less than perfect match for their needs and write extra code to use it.

Java’s interfaces are more often thin than rich. For example, interface CharSequence, which was introduced in Java 1.4, is a thin interface common to all string-like classes that hold a sequence of characters. Here’s its definition when seen as a Scala trait:

trait CharSequence {

def charAt(index: Int): Char def length: Int

def subSequence(start: Int, end: Int): CharSequence def toString(): String

}

Although most of the dozens of methods in class String would apply to any CharSequence, Java’s CharSequence interface declares only four methods. Had CharSequence instead included the full String interface, it would have placed a large burden on implementers of CharSequence. Every programmer that implemented CharSequence in Java would have had to define dozens more methods. Because Scala traits can contain concrete methods, they make rich interfaces far more convenient.

Adding a concrete method to a trait tilts the thin-rich trade-off heavily towards rich interfaces. Unlike in Java, adding a concrete method to a Scala trait is a one-time effort. You only need to implement the method once, in the trait itself, instead of needing to reimplement it for every class that mixes in the trait. Thus, rich interfaces are less work to provide in Scala than in a language without traits.

To enrich an interface using traits, simply define a trait with a small number of abstract methods—the thin part of the trait’s interface—and a potentially large number of concrete methods, all implemented in terms of the abstract methods. Then you can mix the enrichment trait into a class, implement the thin portion of the interface, and end up with a class that has all of the rich interface available.

12.3Example: Rectangular objects

Graphics libraries often have many different classes that represent something rectangular. Some examples are windows, bitmap images, and regions se-

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index

Section 12.3

Chapter 12 · Traits

263

lected with a mouse. To make these rectangular objects convenient to use, it is nice if the library provides geometric queries such as width, height, left, right, topLeft, and so on. However, many such methods exist that would be nice to have, so it can be a large burden on library writers to provide all of them for all rectangular objects in a Java library. If such a library were written in Scala, by contrast, the library writer could use traits to easily supply all of these convenience methods on all the classes they’d like.

To see how, first imagine what the code would look like without traits. There would be some basic geometric classes like Point and Rectangle:

class Point(val x: Int, val y: Int)

class Rectangle(val topLeft: Point, val bottomRight: Point) { def left = topLeft.x

def right = bottomRight.x def width = right - left

// and many more geometric methods...

}

This Rectangle class takes two points in its primary constructor: the coordinates of the top-left and bottom-right corners. It then implements many convenience methods such as left, right, and width by performing simple calculations on these two points.

Another class a graphics library might have is a 2-D graphical widget:

abstract class Component { def topLeft: Point

def bottomRight: Point

def left = topLeft.x

def right = bottomRight.x def width = right - left

// and many more geometric methods...

}

Notice that the definitions of left, right, and width are exactly the same in the two classes. They will also be the same, aside from minor variations, in any other classes for rectangular objects.

This repetition can be eliminated with an enrichment trait. The trait will have two abstract methods: one that returns the top-left coordinate of the object, and another that returns the bottom-right coordinate. It can then supply

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Section 12.3

Chapter 12 · Traits

264

concrete implementations of all the other geometric queries. Listing 12.5 shows what it will look like:

trait Rectangular { def topLeft: Point

def bottomRight: Point

def left = topLeft.x

def right = bottomRight.x def width = right - left

// and many more geometric methods...

}

Listing 12.5 · Defining an enrichment trait.

Class Component can mix in this trait to get all the geometric methods provided by Rectangular:

abstract class Component extends Rectangular { // other methods...

}

Similarly, Rectangle itself can mix in the trait:

class Rectangle(val topLeft: Point, val bottomRight: Point) extends Rectangular {

// other methods...

}

Given these definitions, you can create a Rectangle and call geometric methods such as width and left on it:

scala> val rect = new Rectangle(new Point(1, 1), new Point(10, 10))

rect: Rectangle = Rectangle@3536fd

scala> rect.left res2: Int = 1

scala> rect.right res3: Int = 10

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index

Section 12.4

Chapter 12 · Traits

265

scala> rect.width res4: Int = 9

12.4 The Ordered trait

Comparison is another domain where a rich interface is convenient. Whenever you compare two objects that are ordered, it is convenient if you use a single method call to ask about the precise comparison you want. If you want “is less than,” you would like to call <, and if you want “is less than or equal,” you would like to call <=. With a thin comparison interface, you might just have the < method, and you would sometimes have to write things like “(x < y) || (x == y)”. A rich interface would provide you with all of the usual comparison operators, thus allowing you to directly write things like “x <= y”.

Before looking at Ordered, imagine what you might do without it. Suppose you took the Rational class from Chapter 6 and added comparison operations to it. You would end up with something like this:1

class Rational(n: Int, d: Int) { // ...

def < (that: Rational) =

this.numer * that.denom > that.numer * this.denom def > (that: Rational) = that < this

def <= (that: Rational) = (this < that) || (this == that) def >= (that: Rational) = (this > that) || (this == that)

}

This class defines four comparison operators (<, >, <=, and >=), and it’s a classic demonstration of the costs of defining a rich interface. First, notice that three of the comparison operators are defined in terms of the first one. For example, > is defined as the reverse of <, and <= is defined as literally “less than or equal.” Additionally, notice that all three of these methods would be the same for any other class that is comparable. There is nothing special about rational numbers regarding <=. In a comparison context, <= is always used to mean “less than or equals.” Overall, there is quite a lot of

1The full code for the Rational class on which this example is based is shown in Listing 6.5 on page 155.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Section 12.4

Chapter 12 · Traits

266

boilerplate code in this class which would be the same in any other class that implements comparison operations.

This problem is so common that Scala provides a trait to help with it. The trait is called Ordered. To use it, you replace all of the individual comparison methods with a single compare method. The Ordered trait then defines <, >, <=, and >= for you in terms of this one method. Thus, trait Ordered allows you to enrich a class with comparison methods by implementing only one method, compare.

Here is how it looks if you define comparison operations on Rational by using the Ordered trait:

class Rational(n: Int, d: Int) extends Ordered[Rational] { // ...

def compare(that: Rational) =

(this.numer * that.denom) - (that.numer * this.denom)

}

There are just two things to do. First, this version of Rational mixes in the Ordered trait. Unlike the traits you have seen so far, Ordered requires you to specify a type parameter when you mix it in. Type parameters are not discussed in detail until Chapter 19, but for now all you need to know is that when you mix in Ordered, you must actually mix in Ordered[C], where C is the class whose elements you compare. In this case, Rational mixes in

Ordered[Rational].

The second thing you need to do is define a compare method for comparing two objects. This method should compare the receiver, this, with the object passed as an argument to the method. It should return an integer that is zero if the objects are the same, negative if receiver is less than the argument, and positive if the receiver is greater than the argument. In this case, the comparison method of Rational uses a formula based on converting the fractions to a common denominator and then subtracting the resulting numerators. Given this mixin and the definition of compare, class Rational now has all four comparison methods:

scala> val half = new Rational(1, 2) half: Rational = 1/2

scala> val third = new Rational(1, 3) third: Rational = 1/3

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index

Section 12.5

Chapter 12 · Traits

267

scala> half < third res5: Boolean = false

scala> half > third res6: Boolean = true

Any time you implement a class that is ordered by some comparison, you should consider mixing in the Ordered trait. If you do, you will provide the class’s users with a rich set of comparison methods.

Beware that the Ordered trait does not define equals for you, because it is unable to do so. The problem is that implementing equals in terms of compare requires checking the type of the passed object, and because of type erasure, Ordered itself cannot do this test. Thus, you need to define equals yourself, even if you inherit Ordered. You’ll find out how to go about this in Chapter 30.

12.5 Traits as stackable modifications

You have now seen one major use of traits: turning a thin interface into a rich one. Now we’ll turn to a second major use: providing stackable modifications to classes. Traits let you modify the methods of a class, and they do so in a way that allows you to stack those modifications with each other.

As an example, consider stacking modifications to a queue of integers. The queue will have two operations: put, which places integers in the queue, and get, which takes them back out. Queues are first-in, first-out, so get should return the integers in the same order they were put in the queue.

Given a class that implements such a queue, you could define traits to perform modifications such as these:

Doubling: double all integers that are put in the queue

Incrementing: increment all integers that are put in the queue

Filtering: filter out negative integers from a queue

These three traits represent modifications, because they modify the behavior of an underlying queue class rather than defining a full queue class themselves. The three are also stackable. You can select any of the three you like, mix them into a class, and obtain a new class that has all of the modifications you chose.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index