Programming-In-Scala.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Cover · Overview · Contents · Discuss · Suggest · Glossary · Index Programming in Scala Cover · Overview · Contents · Discuss · Suggest · Glossary · Index Programming in Scala Martin Odersky, Lex Spoon, Bill Venners artima ARTIMA PRESS MOUNTAIN VIEW,CALIFORNIA Cover · Overview · Contents · Discuss · Suggest · Glossary · Index iv Programming in Scala First Edition, Version 6 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. First edition published as PrePrint™ eBook 2007 First edition published 2008 Produced in the United States of America 12 11 10 09 08 5 6 7 8 9 ISBN-10: 0-9815316-1-X ISBN-13: 978-0-9815316-1-8 No part of this publication may be reproduced, modified, 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. Cover · Overview · Contents · Discuss · Suggest · Glossary · Index to Nastaran - M.O. to Fay - L.S. to Siew - B.V. Cover · Overview · Contents · Discuss · Suggest · Glossary · Index Overview Contents vii List of Figures xvii List of Tables xix List of Listings xx Foreword xxvii Acknowledgments xxix Introduction xxxi 1. A Scalable Language 39 2. First Steps in Scala 58 3. Next Steps in Scala 71 4. Classes and Objects 93 5. Basic Types and Operations 107 6. Functional Objects 129 7. Built-in Control Structures 149 8. Functions and Closures 173 9. Control Abstraction 194 10. Composition and Inheritance 209 11. Scala’s Hierarchy 237 12. Traits 245 13. Packages and Imports 264 14. Assertions and Unit Testing 279 15. Case Classes and Pattern Matching 293 16. Working with Lists 327 17. Collections 359 18. Stateful Objects 387 19. Type Parameterization 410 20. Abstract Members 435 21. Implicit Conversions and Parameters 465 22. Implementing Lists 487 23. For Expressions Revisited 500 24. Extractors 516 25. Annotations 532 26. Working with XML 539 27. Modular Programming Using Objects 553 28. Object Equality 568 29. Combining Scala and Java 594 30. Actors and Concurrency 607 31. Combinator Parsing 642 32. GUI Programming 671 33. The SCells Spreadsheet 683 A. Scala scripts on Unix and Windows 708 Glossary 709 Bibliography 725 About the Authors 728 Index 729 Cover · Overview · Contents · Discuss · Suggest · Glossary · Index Contents Contents vii List of Figures xvii List of Tables xix List of Listings xx Foreword xxvii Acknowledgments xxix Introduction xxxi 1 A Scalable Language 39 1.1 A language that grows on you . 40 1.2 What makes Scala scalable? . 45 1.3 Why Scala? ........................ 48 1.4 Scala’s roots ....................... 55 1.5 Conclusion ........................ 57 2 First Steps in Scala 58 Step 1. Learn to use the Scala interpreter . 58 Step 2. Define some variables . 60 Step 3. Define some functions . 62 Step 4. Write some Scala scripts . 64 Step 5. Loop with while; decide with if . 65 Step 6. Iterate with foreach and for . 67 Conclusion ........................... 70 Cover · Overview · Contents · Discuss · Suggest · Glossary · Index Contents viii 3 Next Steps in Scala 71 Step 7. Parameterize arrays with types . 71 Step 8. Use lists ........................ 75 Step 9. Use tuples ....................... 80 Step 10. Use sets and maps ................... 81 Step 11. Learn to recognize the functional style . 86 Step 12. Read lines from a file . 89 Conclusion ........................... 92 4 Classes and Objects 93 4.1 Classes, fields, and methods . 93 4.2 Semicolon inference ................... 98 4.3 Singleton objects ..................... 99 4.4 A Scala application . 102 4.5 The Application trait . 105 4.6 Conclusion . 106 5 Basic Types and Operations 107 5.1 Some basic types . 107 5.2 Literals . 108 5.3 Operators are methods . 115 5.4 Arithmetic operations . 118 5.5 Relational and logical operations . 119 5.6 Bitwise operations . 121 5.7 Object equality . 123 5.8 Operator precedence and associativity . 124 5.9 Rich wrappers . 127 5.10 Conclusion . 127 6 Functional Objects 129 6.1 A specification for class Rational . 129 6.2 Constructing a Rational . 130 6.3 Reimplementing the toString method . 132 6.4 Checking preconditions . 133 6.5 Adding fields . 133 6.6 Self references . 135 6.7 Auxiliary constructors . 136 6.8 Private fields and methods . 138 Cover · Overview · Contents · Discuss · Suggest · Glossary · Index Contents ix 6.9 Defining operators . 139 6.10 Identifiers in Scala . 141 6.11 Method overloading . 144 6.12 Implicit conversions . 146 6.13 A word of caution . 147 6.14 Conclusion . 147 7 Built-in Control Structures 149 7.1 If expressions . 150 7.2 While loops . 151 7.3 For expressions . 154 7.4 Exception handling with try expressions . 159 7.5 Match expressions . 163 7.6 Living without break and continue . 165 7.7 Variable scope . 167 7.8 Refactoring imperative-style code . 170 7.9 Conclusion . 172 8 Functions and Closures 173 8.1 Methods . 173 8.2 Local functions . 175 8.3 First-class functions . 177 8.4 Short forms of function literals . 179 8.5 Placeholder syntax . 180 8.6 Partially applied functions . 181 8.7 Closures . 184 8.8 Repeated parameters . 188 8.9 Tail recursion . 189 8.10 Conclusion . 193 9 Control Abstraction 194 9.1 Reducing code duplication . 194 9.2 Simplifying client code . 198 9.3 Currying . 200 9.4 Writing new control structures . 202 9.5 By-name parameters . 205 9.6 Conclusion . 208 Cover · Overview · Contents · Discuss · Suggest · Glossary · Index Contentsx 10 Composition and Inheritance 209 10.1 A two-dimensional layout library . 209 10.2 Abstract classes . 210 10.3 Defining parameterless methods . 211 10.4 Extending classes . 214 10.5 Overriding methods and fields . 216 10.6 Defining parametric fields . 217 10.7 Invoking superclass constructors . 219 10.8 Using override modifiers . 220 10.9 Polymorphism and dynamic binding . 222 10.10 Declaring final members . 224 10.11 Using composition and inheritance . 226 10.12 Implementing above, beside, and toString . 227 10.13 Defining a factory object . 229 10.14 Heighten and widen . 231 10.15 Putting it all together . 235 10.16 Conclusion . 236 11 Scala’s Hierarchy 237 11.1 Scala’s class hierarchy . 237 11.2 How primitives are implemented . 241 11.3 Bottom types . 243 11.4 Conclusion . 244 12 Traits 245 12.1 How traits work . 245 12.2 Thin versus rich interfaces . 248 12.3 Example: Rectangular objects . 249 12.4 The Ordered trait . 252 12.5 Traits as stackable modifications . 254 12.6 Why not multiple inheritance? . 258 12.7 To trait, or not to trait? . 262 12.8 Conclusion . 263 13 Packages and Imports 264 13.1 Packages . 264 13.2 Imports . 268 13.3 Implicit imports . 272 Cover · Overview · Contents · Discuss · Suggest · Glossary · Index Contents xi 13.4 Access modifiers . 273 13.5 Conclusion . 278 14 Assertions and Unit Testing 279 14.1 Assertions . 279 14.2 Unit testing in Scala . 281 14.3 Informative failure reports . 282 14.4 Using JUnit and TestNG . 284 14.5 Tests as specifications . 286 14.6 Property-based testing . 288 14.7 Organizing and running tests . 290 14.8 Conclusion . 292 15 Case Classes and Pattern Matching 293 15.1 A simple example . 293 15.2 Kinds of patterns . 298 15.3 Pattern guards . 307 15.4 Pattern overlaps . 309 15.5 Sealed classes . 310 15.6 The Option type . 312 15.7 Patterns everywhere . 314 15.8 A larger example . 318 15.9 Conclusion . 326 16 Working with Lists 327 16.1 List literals . 327 16.2 The List type . 328 16.3 Constructing lists . 328 16.4 Basic operations on lists . 329 16.5 List patterns . 330 16.6 First-order methods on class List . 332 16.7 Higher-order methods on class List . 343 16.8 Methods of the List object . 351 16.9 Understanding Scala’s type inference algorithm . 355 16.10 Conclusion . 358 17 Collections 359 17.1 Overview of the library . 359.