programming in scala

Download Programming in Scala

Post on 26-Oct-2014

65 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Good book on scala.

TRANSCRIPT

Cover Overview Contents Discuss Suggest Glossary Index

Programming in Scala

Cover Overview Contents Discuss Suggest Glossary Index

Programming in ScalaMartin Odersky, Lex Spoon, Bill Venners

artimaA RTIMA P RESSM OUNTAIN V IEW, C ALIFORNIA

Cover Overview Contents Discuss Suggest Glossary Index

ivProgramming 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 56789

ISBN-10: 0-9815316-1-X ISBN-13: 978-0-9815316-1-8 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.

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

OverviewContents List of Figures List of Tables List of Listings Foreword 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. Modular Programming Using Objects 28. Object Equality 29. Combining Scala and Java 30. Actors and Concurrency 31. Combinator Parsing 32. GUI Programming 33. The SCells Spreadsheet A. Scala scripts on Unix and Windows Glossary Bibliography About the Authors Indexvii xvii xix xx xxvii xxix xxxi 39 58 71 93 107 129 149 173 194 209 237 245 264 279 293 327 359 387 410 435 465 487 500 516 532 539 553 568 594 607 642 671 683 708 709 725 728 729

Cover Overview Contents Discuss Suggest Glossary Index

Contents

Contents List of Figures List of Tables List of Listings Foreword 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 . . . . . . . . . .

vii xvii xix xx xxvii xxix xxxi 39 40 45 48 55 57 58 58 60 62 64 65 67 70

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

2

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 . . . . . . . . . . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

Cover Overview Contents Discuss Suggest Glossary Index

Contents 3 Next Steps in Scala Step 7. Parameterize arrays with types . . . . Step 8. Use lists . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . Classes and Objects 4.1 Classes, elds, and methods 4.2 Semicolon inference . . . . 4.3 Singleton objects . . . . . . 4.4 A Scala application . . . . 4.5 The Application trait . . . 4.6 Conclusion . . . . . . . . . 71 71 75 80 81 86 89 92 93 93 98 99 102 105 106 107 107 108 115 118 119 121 123 124 127 127 129 129 130 132 133 133 135 136 138

viii

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

4

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

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 Bitwise operations . . . . . . . . . . 5.7 Object equality . . . . . . . . . . . . 5.8 Operator precedence and associativity 5.9 Rich wrappers . . . . . . . . . . . . 5.10 Conclusion . . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

6

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 . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

Cover Overview Contents Discuss Suggest Glossary Index

Contents 6.9 6.10 6.11 6.12 6.13 6.14 7 Dening operators . Identiers in Scala . Method overloading Implicit conversions A word of caution . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 141 144 146 147 147 149 150 151 154 159 163 165 167 170 172 173 173 175 177 179 180 181 184 188 189 193 194 194 198 200 202 205 208

ix

Built-in Control Structures 7.1 If expressions . . . . . . . . . . . . . . 7.2 While loops . . . . . . . . . . . . . . . 7.3 For expressions . . . . . . . . . . . . . . 7.4 Exception handling with try expressions 7.5 Match expressions . . . . . . . . . . . . 7.6 Living without break and continue . . 7.7 Variable scope . . . . . . . . . . . . . . 7.8 Refactoring imperative-style code . . . . 7.9 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 . . . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

8

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

9

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

Cover Overview Contents Discuss Suggest Glossary Index

Contents 10 Composition and Inheritance 10.1 A two-dimensional layout library . . . . . . 10.2 Abstract classes . . . . . . . . . . . . . . . 10.3 Dening parameterless methods . . . . . . . 10.4 Extending classes . . . . . . . . . . . . . . 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.10 Decl