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

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

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

Добавлен: 02.01.2026

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

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

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

 

Contents

xiv

9.5

By-name parameters . . . . . . . . . . . . . . . . . . .

218

9.6

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

221

10 Composition and Inheritance

222

10.1

A two-dimensional layout library . . . . . . . . . . . .

222

10.2

Abstract classes . . . . . . . . . . . . . . . . . . . . .

223

10.3

Defining parameterless methods . . . . . . . . . . . . .

224

10.4

Extending classes . . . . . . . . . . . . . . . . . . . .

227

10.5

Overriding methods and fields . . . . . . . . . . . . . .

229

10.6

Defining parametric fields . . . . . . . . . . . . . . . .

230

10.7

Invoking superclass constructors . . . . . . . . . . . . .

232

10.8

Using override modifiers . . . . . . . . . . . . . . . .

233

10.9

Polymorphism and dynamic binding . . . . . . . . . .

235

10.10

Declaring final members . . . . . . . . . . . . . . . . .

237

10.11

Using composition and inheritance . . . . . . . . . . .

239

10.12

Implementing above, beside, and toString . . . . . .

240

10.13

Defining a factory object . . . . . . . . . . . . . . . . .

242

10.14

Heighten and widen . . . . . . . . . . . . . . . . . . .

244

10.15

Putting it all together . . . . . . . . . . . . . . . . . . .

248

10.16

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

249

11 Scala’s Hierarchy

250

11.1

Scala’s class hierarchy . . . . . . . . . . . . . . . . . .

250

11.2

How primitives are implemented . . . . . . . . . . . .

254

11.3

Bottom types . . . . . . . . . . . . . . . . . . . . . . .

256

11.4

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

257

12 Traits

 

258

12.1

How traits work . . . . . . . . . . . . . . . . . . . . .

258

12.2

Thin versus rich interfaces . . . . . . . . . . . . . . . .

261

12.3

Example: Rectangular objects . . . . . . . . . . . . . .

262

12.4

The Ordered trait . . . . . . . . . . . . . . . . . . . .

265

12.5

Traits as stackable modifications . . . . . . . . . . . . .

267

12.6

Why not multiple inheritance? . . . . . . . . . . . . . .

271

12.7

To trait, or not to trait? . . . . . . . . . . . . . . . . . .

275

12.8

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

276

13 Packages and Imports

277

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


 

Contents

xv

13.1

Putting code in packages . . . . . . . . . . . . . . . . .

277

13.2

Concise access to related code . . . . . . . . . . . . . .

278

13.3

Imports . . . . . . . . . . . . . . . . . . . . . . . . . .

282

13.4

Implicit imports . . . . . . . . . . . . . . . . . . . . .

286

13.5

Access modifiers . . . . . . . . . . . . . . . . . . . . .

287

13.6

Package objects . . . . . . . . . . . . . . . . . . . . .

292

13.7

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

294

14 Assertions and Unit Testing

295

14.1

Assertions . . . . . . . . . . . . . . . . . . . . . . . .

295

14.2

Unit testing in Scala . . . . . . . . . . . . . . . . . . .

297

14.3

Informative failure reports . . . . . . . . . . . . . . . .

298

14.4

Using JUnit and TestNG . . . . . . . . . . . . . . . . .

300

14.5

Tests as specifications . . . . . . . . . . . . . . . . . .

302

14.6

Property-based testing . . . . . . . . . . . . . . . . . .

305

14.7

Organizing and running tests . . . . . . . . . . . . . .

306

14.8

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

308

15 Case Classes and Pattern Matching

309

15.1

A simple example . . . . . . . . . . . . . . . . . . . .

309

15.2

Kinds of patterns . . . . . . . . . . . . . . . . . . . . .

314

15.3

Pattern guards . . . . . . . . . . . . . . . . . . . . . .

324

15.4

Pattern overlaps . . . . . . . . . . . . . . . . . . . . .

325

15.5

Sealed classes . . . . . . . . . . . . . . . . . . . . . .

326

15.6

The Option type . . . . . . . . . . . . . . . . . . . . .

328

15.7

Patterns everywhere . . . . . . . . . . . . . . . . . . .

330

15.8

A larger example . . . . . . . . . . . . . . . . . . . . .

335

15.9

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

343

16 Working with Lists

344

16.1

List literals . . . . . . . . . . . . . . . . . . . . . . . .

344

16.2

The List type . . . . . . . . . . . . . . . . . . . . . .

345

16.3

Constructing lists . . . . . . . . . . . . . . . . . . . . .

345

16.4

Basic operations on lists . . . . . . . . . . . . . . . . .

346

16.5

List patterns . . . . . . . . . . . . . . . . . . . . . . .

347

16.6

First-order methods on class List . . . . . . . . . . . .

349

16.7

Higher-order methods on class List . . . . . . . . . .

361

16.8

Methods of the List object . . . . . . . . . . . . . . .

369

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


 

Contents

xvi

16.9

Processing multiple lists together . . . . . . . . . . . .

371

16.10

Understanding Scala’s type inference algorithm . . . . .

372

16.11

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

376

17 Collections

377

17.1

Sequences . . . . . . . . . . . . . . . . . . . . . . . .

377

17.2

Sets and maps . . . . . . . . . . . . . . . . . . . . . .

381

17.3

Selecting mutable versus immutable collections . . . .

390

17.4

Initializing collections . . . . . . . . . . . . . . . . . .

392

17.5

Tuples . . . . . . . . . . . . . . . . . . . . . . . . . .

396

17.6

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

398

18 Stateful Objects

399

18.1

What makes an object stateful? . . . . . . . . . . . . .

399

18.2

Reassignable variables and properties . . . . . . . . . .

402

18.3

Case study: Discrete event simulation . . . . . . . . . .

405

18.4

A language for digital circuits . . . . . . . . . . . . . .

406

18.5

The Simulation API . . . . . . . . . . . . . . . . . .

409

18.6

Circuit Simulation . . . . . . . . . . . . . . . . . . . .

413

18.7

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

421

19 Type Parameterization

422

19.1

Functional queues . . . . . . . . . . . . . . . . . . . .

422

19.2

Information hiding . . . . . . . . . . . . . . . . . . . .

426

19.3

Variance annotations . . . . . . . . . . . . . . . . . . .

429

19.4

Checking variance annotations . . . . . . . . . . . . . .

433

19.5

Lower bounds . . . . . . . . . . . . . . . . . . . . . .

436

19.6

Contravariance . . . . . . . . . . . . . . . . . . . . . .

438

19.7

Object private data . . . . . . . . . . . . . . . . . . . .

441

19.8

Upper bounds . . . . . . . . . . . . . . . . . . . . . .

443

19.9

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

446

20 Abstract Members

447

20.1

A quick tour of abstract members . . . . . . . . . . . .

447

20.2

Type members . . . . . . . . . . . . . . . . . . . . . .

448

20.3

Abstract vals . . . . . . . . . . . . . . . . . . . . . . .

449

20.4

Abstract vars . . . . . . . . . . . . . . . . . . . . . . .

450

20.5

Initializing abstract vals . . . . . . . . . . . . . . . . .

451

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


 

Contents

xvii

20.6

Abstract types . . . . . . . . . . . . . . . . . . . . . .

459

20.7

Path-dependent types . . . . . . . . . . . . . . . . . .

461

20.8

Structural subtyping . . . . . . . . . . . . . . . . . . .

464

20.9

Enumerations . . . . . . . . . . . . . . . . . . . . . . .

466

20.10

Case study: Currencies . . . . . . . . . . . . . . . . . .

468

20.11

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

478

21 Implicit Conversions and Parameters

479

21.1

Implicit conversions . . . . . . . . . . . . . . . . . . .

479

21.2

Rules for implicits . . . . . . . . . . . . . . . . . . . .

482

21.3

Implicit conversion to an expected type . . . . . . . . .

485

21.4

Converting the receiver . . . . . . . . . . . . . . . . .

486

21.5

Implicit parameters . . . . . . . . . . . . . . . . . . . .

489

21.6

View bounds . . . . . . . . . . . . . . . . . . . . . . .

495

21.7

When multiple conversions apply . . . . . . . . . . . .

498

21.8

Debugging implicits . . . . . . . . . . . . . . . . . . .

501

21.9

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

502

22 Implementing Lists

503

22.1

The List class in principle . . . . . . . . . . . . . . .

503

22.2

The ListBuffer class . . . . . . . . . . . . . . . . . .

509

22.3

The List class in practice . . . . . . . . . . . . . . . .

511

22.4

Functional on the outside . . . . . . . . . . . . . . . .

513

22.5

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

514

23 For Expressions Revisited

516

23.1

For expressions . . . . . . . . . . . . . . . . . . . . . .

517

23.2

The n-queens problem . . . . . . . . . . . . . . . . . .

519

23.3

Querying with for expressions . . . . . . . . . . . . .

522

23.4

Translation of for expressions . . . . . . . . . . . . . .

524

23.5

Going the other way . . . . . . . . . . . . . . . . . . .

528

23.6

Generalizing for . . . . . . . . . . . . . . . . . . . . .

529

23.7

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

531

24 The Scala Collections API

532

24.1

Mutable and immutable collections . . . . . . . . . . .

533

24.2

Collections consistency . . . . . . . . . . . . . . . . .

535

24.3

Trait Traversable . . . . . . . . . . . . . . . . . . . .

537

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


 

Contents

xviii

24.4

Trait Iterable . . . . . . . . . . . . . . . . . . . . . .

542

24.5

The sequence traits Seq, IndexedSeq, and LinearSeq .

546

24.6

Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . .

551

24.7

Maps . . . . . . . . . . . . . . . . . . . . . . . . . . .

557

24.8

Synchronized sets and maps . . . . . . . . . . . . . . .

562

24.9

Concrete immutable collection classes . . . . . . . . .

564

24.10

Concrete mutable collection classes . . . . . . . . . . .

571

24.11

Arrays . . . . . . . . . . . . . . . . . . . . . . . . . .

578

24.12

Strings . . . . . . . . . . . . . . . . . . . . . . . . . .

583

24.13

Performance characteristics . . . . . . . . . . . . . . .

584

24.14

Equality . . . . . . . . . . . . . . . . . . . . . . . . .

585

24.15

Views . . . . . . . . . . . . . . . . . . . . . . . . . . .

587

24.16

Iterators . . . . . . . . . . . . . . . . . . . . . . . . .

593

24.17

Creating collections from scratch . . . . . . . . . . . .

601

24.18

Conversions between Java and Scala collections . . . .

603

24.19

Migrating from Scala 2.7 . . . . . . . . . . . . . . . .

605

24.20

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

606

25 The Architecture of Scala Collections

607

25.1

Builders . . . . . . . . . . . . . . . . . . . . . . . . .

608

25.2

Factoring out common operations . . . . . . . . . . . .

609

25.3

Integrating new collections . . . . . . . . . . . . . . .

614

25.4

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

630

26 Extractors

631

26.1

An example: extracting email addresses . . . . . . . . .

631

26.2

Extractors . . . . . . . . . . . . . . . . . . . . . . . .

632

26.3

Patterns with zero or one variables . . . . . . . . . . .

635

26.4

Variable argument extractors . . . . . . . . . . . . . . .

637

26.5

Extractors and sequence patterns . . . . . . . . . . . .

640

26.6

Extractors versus case classes . . . . . . . . . . . . . .

641

26.7

Regular expressions . . . . . . . . . . . . . . . . . . .

642

26.8

Conclusion . . . . . . . . . . . . . . . . . . . . . . . .

646

27 Annotations

647

27.1

Why have annotations? . . . . . . . . . . . . . . . . .

647

27.2

Syntax of annotations . . . . . . . . . . . . . . . . . .

648

27.3

Standard annotations . . . . . . . . . . . . . . . . . . .

650

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