scala book

Download Scala Book

Post on 30-Sep-2014

174 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

A comprehensive step-by-step guide

Programming in

ScalaMartin Odersky Lex Spoon Bill Venners

Prepared for Burri Grgory

artima

Cover Overview Contents Discuss Suggest Glossary Index

Programming in ScalaPrePrint Edition

Prepared for Burri Grgory

Cover Overview Contents Discuss Suggest Glossary Index

iii Thank you for purchasing the PrePrint Edition of Programming in Scala. A PrePrint is a work-in-progress, a book that has not yet been fully written, reviewed, edited, or formatted. We are publishing this book as a PrePrint for two main reasons. First, even though this book is not quite nished, the information contained in its pages can already provide value to many readers. Second, we hope to get reports of errata and suggestions for improvement from those readers while we still have time to incorporate them into the rst printing. As a PrePrint customer, youll be able to download new PrePrint versions from Artima as the book evolves, as well as the nal PDF of the book once nished. Youll have access to the books content prior to its print publication, and can participate in its creation by submitting feedback. Please submit by clicking on the Suggest link at the bottom of each page. Thanks for your participation. We hope you nd the book useful and enjoyable. Bill Venners President, Artima, Inc.

Prepared for Burri Grgory

Cover Overview Contents Discuss Suggest Glossary Index

Programming in ScalaPrePrint Edition Martin Odersky, Lex Spoon, Bill Venners

Foreword by Neal Gafter

Prepared for Burri Grgory

artimaA RTIMA P RESSM OUNTAIN V IEW, C ALIFORNIA

Cover Overview Contents Discuss Suggest Glossary Index

vProgramming in Scala PrePrint Edition Version 3 Martin Odersky is the creator of the Scala language and a professor at EPFL in Lausanne, Switzerland. Lex Spoon worked on Scala for two years as a post-doc with Martin Odersky. Bill Venners is president of Artima, Inc. Artima Press is an imprint of Artima, Inc. P.O. Box 390122, Mountain View, California 94039 Copyright 2007, 2008 Martin Odersky, Lex Spoon, and Bill Venners. All rights reserved. PrePrint Edition rst published 2007 Version 3 published May 4, 2008 Produced in the United States of America 12 11 10 09 08 34567

ISBN-10: 0-9815316-0-1 ISBN-13: 978-0-9815316-0-1 No part of this publication may be reproduced, modied, distributed, stored in a retrieval system, republished, displayed, or performed, for commercial or noncommercial purposes or for compensation of any kind without prior written permission from Artima, Inc. All information and materials in this book are provided "as is" and without warranty of any kind. The term Artima and the Artima logo are trademarks or registered trademarks of Artima, Inc. All other company and/or product names may be trademarks or registered trademarks of their owners.

Prepared for Burri Grgory

Cover Overview Contents Discuss Suggest Glossary Index

to Nastaran - M.O. to Fay - L.S. to Siew - B.V.

Prepared for Burri Grgory

Cover Overview Contents Discuss Suggest Glossary Index

OverviewContents Foreword Preface Acknowledgments Introduction 1. A Scalable Language 2. First Steps in Scala 3. Next Steps in Scala 4. Classes and Objects 5. Basic Types and Operations 6. Functional Objects 7. Built-in Control Structures 8. Functions and Closures 9. Control Abstraction 10. Composition and Inheritance 11. Scalas Hierarchy 12. Traits 13. Packages and Imports 14. Assertions and Unit Testing 15. Case Classes and Pattern Matching 16. Working with Lists 17. Collections 18. Stateful Objects 19. Type Parameterization 20. Abstract Members 21. Implicit Conversions and Parameters 22. Implementing Lists 23. For-Expressions Revisited 24. Extractors 25. Annotations 26. Working with XML 27. Objects As Modules 28. Object Equality 29. Combining Scala and Java 30. Actors and Concurrency 31. Combinator Parsing 32. GUI Programming 33. The SCells Spreadsheet Glossary Bibliography About the Authors Indexviii xvii xix xx xxii 29 48 62 83 100 121 140 157 178 193 218 226 243 257 267 298 328 349 369 387 404 422 433 448 463 469 482 493 507 519 550 579 590 615 630 632 634

Prepared for Burri Grgory

Cover Overview Contents Discuss Suggest Glossary Index

Contents

Contents Foreword Preface Acknowledgments Introduction 1 A Scalable Language 1.1 A language that grows on you 1.2 What makes Scala scalable? . 1.3 Why Scala? . . . . . . . . . . 1.4 Scalas roots . . . . . . . . . . 1.5 Conclusion . . . . . . . . . .

viii xvii xix xx xxii 29 30 35 38 45 47 48 48 50 52 55 57 59 61 62 62 66

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

2

Prepared for Burri Grgory

First Steps in Scala Step 1. Learn to use the Scala interpreter Step 2. Dene some variables . . . . . . Step 3. Dene some functions . . . . . Step 4. Write some Scala scripts . . . . Step 5. Loop with while, decide with if Step 6. Iterate with foreach and for . . Conclusion . . . . . . . . . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

3

Next Steps in Scala Step 7. Parameterize Arrays with types . . . . . . . . . . . . . . Step 8. Use Lists . . . . . . . . . . . . . . . . . . . . . . . . . . Cover Overview Contents Discuss Suggest Glossary Index

Contents Step 9. Use Tuples . . . . . . . . . . . . . . . Step 10. Use Sets and Maps . . . . . . . . . . . Step 11. Learn to recognize the functional style Step 12. Read lines from a le . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . 4 Classes and Objects 4.1 Classes, elds, and methods 4.2 Semicolon inference . . . . 4.3 Variable scope . . . . . . . . 4.4 Singleton objects . . . . . . 4.5 A Scala application . . . . . 4.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 72 76 79 82 83 83 89 90 94 97 99 100 101 102 107 110 112 114 115 117 119 119 121 121 122 123 124 125 127 128 129 131 133 135

ix

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

5

Basic Types and Operations 5.1 Some basic types . . . . . . . . . . . 5.2 Literals . . . . . . . . . . . . . . . . 5.3 Operators are methods . . . . . . . . 5.4 Arithmetic operations . . . . . . . . . 5.5 Relational and logical operations . . . 5.6 Object equality . . . . . . . . . . . . 5.7 Bitwise operations . . . . . . . . . . 5.8 Operator precedence and associativity 5.9 Rich wrappers . . . . . . . . . . . . . 5.10 Conclusion . . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

6

Prepared for Burri Grgory

Functional Objects 6.1 A specication for class Rational . . . 6.2 Constructing a Rational . . . . . . . . 6.3 Reimplementing the toString method 6.4 Checking preconditions . . . . . . . . . 6.5 Adding elds . . . . . . . . . . . . . . 6.6 Self references . . . . . . . . . . . . . 6.7 Auxiliary constructors . . . . . . . . . 6.8 Private elds and methods . . . . . . . 6.9 Dening operators . . . . . . . . . . . 6.10 Identiers in Scala . . . . . . . . . . . 6.11 Method overloading . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

Cover Overview Contents Discuss Suggest Glossary Index

Contents 6.12 Implicit conversions . . . . . . . . . . . . . . . . . . . . . . 137 6.13 A word of caution . . . . . . . . . . . . . . . . . . . . . . . 138 6.14 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 139 7 Built-in Control Structures 7.1 If expressions . . . . . . . . . . . . 7.2 While loops . . . . . . . . . . . . . 7.3 For expressions . . . . . . . . . . . 7.4 Try expressions . . . . . . . . . . . 7.5 Match expressions . . . . . . . . . 7.6 Living without break and continue 7.7 Conclusion . . . . . . . . . . . . . Functions and Closures 8.1 Methods . . . . . . . . . . . . 8.2 Local functions . . . . . . . . 8.3 First-class functions . . . . . . 8.4 Short forms of function literals 8.5 Placeholder syntax . . . . . . 8.6 Partially applied functions . . 8.7 Closures . . . . . . . . . . . . 8.8 Repeated parameters . . . . . 8.9 Tail recursion . . . . . . . . . 8.10 Conclusion . . . . . . . . . . Control Abstraction 9.1 Reducing code duplication . . 9.2 Simplifying client code . . . . 9.3 Currying . . . . . . . . . . . . 9.4 Writing new control structures 9.5 By-name parameters . . . . . 9.6 Conclusion . . . . . . . . . . 140 141 143 145 150 153 154 156