Scala Book

Download Scala Book

Post on 30-Sep-2014

173 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

<p>A comprehensive step-by-step guide</p> <p>Programming in</p> <p>ScalaMartin Odersky Lex Spoon Bill Venners</p> <p>Prepared for Burri Grgory</p> <p>artima</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Programming in ScalaPrePrint Edition</p> <p>Prepared for Burri Grgory</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>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.</p> <p>Prepared for Burri Grgory</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Programming in ScalaPrePrint Edition Martin Odersky, Lex Spoon, Bill Venners</p> <p>Foreword by Neal Gafter</p> <p>Prepared for Burri Grgory</p> <p>artimaA RTIMA P RESSM OUNTAIN V IEW, C ALIFORNIA</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>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</p> <p>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.</p> <p>Prepared for Burri Grgory</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>to Nastaran - M.O. to Fay - L.S. to Siew - B.V.</p> <p>Prepared for Burri Grgory</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>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</p> <p>Prepared for Burri Grgory</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Contents</p> <p>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 . . . . . . . . . .</p> <p>viii xvii xix xx xxii 29 30 35 38 45 47 48 48 50 52 55 57 59 61 62 62 66</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>2</p> <p>Prepared for Burri Grgory</p> <p>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 . . . . . . . . . . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>3</p> <p>Next Steps in Scala Step 7. Parameterize Arrays with types . . . . . . . . . . . . . . Step 8. Use Lists . . . . . . . . . . . . . . . . . . . . . . . . . . Cover Overview Contents Discuss Suggest Glossary Index</p> <p>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</p> <p>ix</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>5</p> <p>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 . . . . . . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>6</p> <p>Prepared for Burri Grgory</p> <p>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 . . . . . . . . . . .</p> <p>. . . . . . . . . . .</p> <p>. . . . . . . . . . .</p> <p>. . . . . . . . . . .</p> <p>. . . . . . . . . . .</p> <p>. . . . . . . . . . .</p> <p>. . . . . . . . . . .</p> <p>. . . . . . . . . . .</p> <p>. . . . . . . . . . .</p> <p>. . . . . . . . . . .</p> <p>. . . . . . . . . . .</p> <p>. . . . . . . . . . .</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>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 157 157 159 160 162 163 164 168 171 173 177 178 178 182 184 186 189 191 193 193 194 195 198</p> <p>x</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>8</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>9</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>Prepared for Burri Grgory</p> <p>10 Composition and Inheritance 10.1 A two-dimensional layout library 10.2 Abstract classes . . . . . . . . . 10.3 Dening parameterless methods 10.4 Extending classes . . . . . . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Contents 10.5 Overriding methods and elds . . . . . . . . 10.6 Dening parametric elds . . . . . . . . . . . 10.7 Invoking superclass constructors . . . . . . . 10.8 Using Override modiers . . . . . . . . . . . 10.9 Polymorphism and dynamic binding . . . . . 10.10Declaring nal members . . . . . . . . . . . 10.11Using composition and inheritance . . . . . . 10.12Implementing above, beside, and toString 10.13Dening a factory object . . . . . . . . . . . 10.14Putting it all together . . . . . . . . . . . . . 10.15Conclusion . . . . . . . . . . . . . . . . . . 11 Scalas Hierarchy 11.1 Scalas class hierarchy . . . . . 11.2 How primitives are implemented 11.3 Bottom types . . . . . . . . . . 11.4 Conclusion . . . . . . . . . . . 12 Traits 12.1 How traits work . . . . . . . . . 12.2 Thin versus rich interfaces . . . 12.3 Example: Rectangular objects . 12.4 The standard Ordered trait . . . 12.5 Traits as stackable modications 12.6 Why not multiple inheritance? . 12.7 To trait, or not to trait? . . . . . 12.8 Conclusion . . . . . . . . . . . 13 Packages and Imports 13.1 Packages . . . . 13.2 Imports . . . . . 13.3 Implicit imports . 13.4 Access modiers 13.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 201 202 203 205 208 209 209 213 215 217 218 218 222 224 225 226 226 228 229 231 234 238 241 242 243 244 246 250 250 256</p> <p>xi</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>Prepared for Burri Grgory</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>14 Assertions and Unit Testing 257 14.1 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 14.2 Unit testing in Scala . . . . . . . . . . . . . . . . . . . . . . 259 Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Contents 14.3 14.4 14.5 14.6 14.7 Using JUnit and TestNG Using xtures . . . . . . Tests as specications . . Property-based testing . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 263 265 265 266 267 267 271 280 281 282 284 286 290 297 298 298 298 299 300 301 303 313 321 324 327 328 328 330 334 336 340 341 347</p> <p>xii</p> <p>15 Case Classes and Pattern Matching 15.1 A simple example . . . . . . . . 15.2 Kinds of patterns . . . . . . . . 15.3 Pattern guards . . . . . . . . . . 15.4 Pattern overlaps . . . . . . . . . 15.5 Sealed classes . . . . . . . . . . 15.6 The Option type . . . . . . . . 15.7 Patterns everywhere . . . . . . . 15.8 A larger example . . . . . . . . 15.9 Conclusion . . . . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>16 Working with Lists 16.1 List literals . . . . . . . . . . . . . . . . . . . . . . . . 16.2 The List type . . . . . . . . . . . . . . . . . . . . . . . 16.3 Constructing lists . . . . . . . . . . . . . . . . . . . . . 16.4 Basic operations on lists . . . . . . . . . . . . . . . . . 16.5 List patterns . . . . . . . . . . . . . . . . . . . . . . . . 16.6 Operations on lists, Part I: First-order methods . . . . . . 16.7 Operations on lists, Part II: Higher-order met...</p>