
Programming in Scala DRAFT May 21, 2007 Martin Odersky PROGRAMMING METHODS LABORATORY EPFL SWITZERLAND Contents I Rationale 1 II Scala by Example 7 1 A First Example 11 2 Programming with Actors and Messages 15 3 Expressions and Simple Functions 19 3.1 Expressions And Simple Functions ...................... 19 3.2 Parameters .................................... 20 3.3 Conditional Expressions ............................ 23 3.4 Example: Square Roots by Newton’s Method ................ 23 3.5 Nested Functions ................................ 24 3.6 Tail Recursion .................................. 26 4 First-Class Functions 29 4.1 Anonymous Functions ............................. 30 4.2 Currying ..................................... 31 4.3 Example: Finding Fixed Points of Functions ................ 34 4.4 Summary ..................................... 36 4.5 Language Elements Seen So Far ....................... 36 5 Classes and Objects 39 6 Case Classes and Pattern Matching 51 6.1 Case Classes and Case Objects ........................ 54 6.2 Pattern Matching ................................ 55 iv CONTENTS 7 Generic Types and Methods 59 7.1 Type Parameter Bounds ............................ 61 7.2 Variance Annotations .............................. 64 7.3 Lower Bounds .................................. 66 7.4 Least Types .................................... 66 7.5 Tuples ....................................... 68 7.6 Functions .................................... 69 8 Lists 71 8.1 Using Lists .................................... 71 8.2 Definition of class List I: First Order Methods ............... 73 8.3 Example: Merge sort .............................. 76 8.4 Definition of class List II: Higher-Order Methods ............. 78 8.5 Summary ..................................... 85 9 For-Comprehensions 87 9.1 The N-Queens Problem ............................ 88 9.2 Querying with For-Comprehensions ..................... 89 9.3 Translation of For-Comprehensions ..................... 90 9.4 For-Loops .................................... 92 9.5 Generalizing For ................................. 92 10 Mutable State 95 10.1 Stateful Objects ................................. 95 10.2 Imperative Control Structures ......................... 99 10.3 Extended Example: Discrete Event Simulation ............... 100 10.4 Summary ..................................... 105 11 Computing with Streams 107 12 Iterators 111 12.1 Iterator Methods ................................ 111 12.2 Constructing Iterators ............................. 114 12.3 Using Iterators .................................. 115 CONTENTS v 13 Implicit Parameters and Conversions 117 14 Combinator Parsing 121 14.1 Simple Combinator Parsing .......................... 121 14.2 Parsers that Produce Results ......................... 125 15 Hindley/Milner Type Inference 131 16 Abstractions for Concurrency 141 16.1 Signals and Monitors .............................. 141 16.2 SyncVars ..................................... 143 16.3 Futures ...................................... 143 16.4 Parallel Computations ............................. 144 16.5 Semaphores ................................... 145 16.6 Readers/Writers ................................. 145 16.7 Asynchronous Channels ............................ 146 16.8 Synchronous Channels ............................. 147 16.9 Workers ...................................... 148 16.10Mailboxes .................................... 150 16.11Actors ....................................... 153 III The Scala Language Specification Version 1.0 155 17 Lexical Syntax 159 17.1 Identifiers .................................... 160 17.2 Newline Characters ............................... 161 17.3 Literals ...................................... 164 17.3.1 Integer Literals ............................. 164 17.3.2 Floating Point Literals ......................... 165 17.3.3 Boolean Literals ............................. 166 17.3.4 Character Literals ............................ 166 17.3.5 String Literals .............................. 166 17.3.6 Escape Sequences ........................... 167 vi CONTENTS 17.3.7 Symbol literals .............................. 168 17.4 Whitespace and Comments .......................... 168 17.5 XML mode .................................... 169 18 Identifiers, Names and Scopes 171 19 Types 173 19.1 Paths ....................................... 174 19.2 Value Types ................................... 174 19.2.1 Singleton Types ............................. 174 19.2.2 Type Projection ............................. 175 19.2.3 Type Designators ............................ 175 19.2.4 Parameterized Types .......................... 175 19.2.5 Tuple Types ............................... 176 19.2.6 Annotated Types ............................ 177 19.2.7 Compound Types ............................ 177 19.2.8 Infix Types ................................ 177 19.2.9 Function Types ............................. 178 19.2.10Primitive Types Defined in Predef .................. 178 19.3 Non-Value Types ................................ 179 19.3.1 Method Types .............................. 179 19.3.2 Polymorphic Method Types ...................... 180 19.3.3 Type Constructors ........................... 180 19.4 Base Types and Member Definitions ..................... 180 19.5 Relations between types ............................ 182 19.5.1 Type Equivalence ............................ 182 19.5.2 Conformance .............................. 183 19.6 Type Erasure ................................... 185 20 Basic Declarations and Definitions 187 20.1 Value Declarations and Definitions ..................... 187 20.2 Variable Declarations and Definitions .................... 189 20.3 Type Declarations and Type Aliases ..................... 190 20.4 Type Parameters ................................. 192 CONTENTS vii 20.5 Variance Annotations .............................. 193 20.6 Function Declarations and Definitions ................... 195 20.6.1 By-Name Parameters .......................... 196 20.6.2 Repeated Parameters .......................... 197 20.6.3 Procedures ................................ 198 20.6.4 Method Return Type Inference .................... 198 20.7 Import Clauses ................................. 199 21 Classes and Objects 201 21.1 Templates .................................... 201 21.1.1 Constructor Invocations ........................ 203 21.1.2 Class Linearization ........................... 203 21.1.3 Class Members ............................. 204 21.1.4 Overriding ................................ 206 21.1.5 Inheritance Closure .......................... 206 21.1.6 Early Definitions ............................ 207 21.2 Modifiers ..................................... 208 21.3 Class Definitions ................................ 211 21.3.1 Constructor Definitions ........................ 213 21.3.2 Case Classes ............................... 214 21.3.3 Traits ................................... 215 21.4 Object Definitions ............................... 217 22 Expressions 219 22.1 Literals ...................................... 220 22.2 The Null Value .................................. 220 22.3 Designators ................................... 221 22.4 This and Super ................................. 221 22.5 Function Applications ............................. 222 22.6 Method Values .................................. 224 22.7 Type Applications ................................ 224 22.8 Tuples ....................................... 225 22.9 Instance Creation Expressions ........................ 225 22.10Blocks ....................................... 225 viii CONTENTS 22.11Prefix, Infix, and Postfix Operations ..................... 226 22.11.1Prefix Operations ............................ 227 22.11.2Postfix Operations ........................... 227 22.11.3Infix Operations ............................. 227 22.11.4Assignment Operators ......................... 228 22.12Typed Expressions ............................... 228 22.13Annotated Expressions ............................. 229 22.14Assignments ................................... 229 22.15Conditional Expressions ............................ 230 22.16WhileLoop Expressions ............................ 231 22.17DoLoop Expressions .............................. 231 22.18For-Comprehensions .............................. 231 22.19Return Expressions ............................... 234 22.20Throw Expressions ............................... 234 22.21Try Expressions ................................. 234 22.22Anonymous Functions ............................. 235 22.23Statements .................................... 237 22.24Implicit Conversions .............................. 238 22.24.1Value Conversions ........................... 238 22.24.2Method Conversions .......................... 238 22.24.3Overloading Resolution ........................ 239 22.24.4Local Type Inference .......................... 240 22.24.5Eta Expansion .............................. 243 23 Implicit Parameters and Views 245 23.1 The Implicit Modifier .............................. 245 23.2 Implicit Parameters ............................... 246 23.3 Views ....................................... 248 23.4 View Bounds ................................... 249 24 Pattern Matching 251 24.1 Patterns ...................................... 251 24.1.1 Variable Patterns ............................ 252 24.1.2 Typed Patterns ............................. 252 CONTENTS ix 24.1.3 Literal Patterns ............................. 252 24.1.4 Stable Identifier
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages325 Page
-
File Size-