Foreword |
|
xvii | |
Preface |
|
xxi | |
|
1 Zero To Sixty: Introducing Scala |
|
|
1 | (30) |
|
|
1 | (2) |
|
|
2 | (1) |
|
|
3 | (1) |
|
|
3 | (1) |
|
Installing the Scala Tools You Need |
|
|
3 | (1) |
|
Building the Code Examples |
|
|
4 | (1) |
|
|
5 | (4) |
|
|
5 | (2) |
|
Running the Scala Command-Line Tools Using sbt |
|
|
7 | (2) |
|
|
9 | (11) |
|
|
20 | (9) |
|
|
29 | (2) |
|
|
31 | (40) |
|
|
3 | (28) |
|
|
31 | (2) |
|
|
33 | (1) |
|
|
33 | (2) |
|
|
35 | (1) |
|
|
36 | (3) |
|
|
39 | (9) |
|
Method Default and Named Parameters |
|
|
39 | (1) |
|
Methods with Multiple Parameter Lists |
|
|
40 | (5) |
|
Nesting Method Definitions and Recursion |
|
|
45 | (3) |
|
Inferring Type Information |
|
|
48 | (1) |
|
|
49 | (2) |
|
|
51 | (3) |
|
|
54 | (4) |
|
|
54 | (1) |
|
|
55 | (1) |
|
|
56 | (1) |
|
|
56 | (2) |
|
|
58 | (1) |
|
|
58 | (1) |
|
|
58 | (2) |
|
Option, Some, and None: Avoiding Nulls |
|
|
60 | (2) |
|
When You Really Can't Avoid Nulls |
|
|
61 | (1) |
|
Sealed Class Hierarchies and Enumerations |
|
|
62 | (1) |
|
Organizing Code in Files and Namespaces |
|
|
63 | (1) |
|
Importing Types and Their Members |
|
|
64 | (2) |
|
Package Imports and Package Objects |
|
|
65 | (1) |
|
Parameterized Types Versus Abstract Type Members |
|
|
66 | (3) |
|
|
69 | (2) |
|
3 Rounding Out The Basics |
|
|
71 | (34) |
|
|
71 | (5) |
|
Allowed Characters in Identifiers |
|
|
76 | (1) |
|
Methods with Empty Parameter Lists |
|
|
77 | (1) |
|
Operator Precedence Rules |
|
|
78 | (1) |
|
Enumerations and Algebraic Data Types |
|
|
79 | (3) |
|
|
82 | (1) |
|
Scala Conditional Expressions |
|
|
83 | (2) |
|
Conditional and Comparison Operators |
|
|
85 | (1) |
|
|
86 | (4) |
|
|
86 | (1) |
|
|
87 | (1) |
|
|
87 | (1) |
|
|
87 | (1) |
|
Expanded Scope and Value Definitions |
|
|
88 | (2) |
|
|
90 | (1) |
|
Using try, catch, and finally Clauses |
|
|
90 | (4) |
|
Call by Name, Call by Value |
|
|
94 | (3) |
|
|
97 | (2) |
|
Traits: Interfaces and Mixins in Scala |
|
|
99 | (3) |
|
|
102 | (1) |
|
|
103 | (2) |
|
|
105 | (32) |
|
Safer Pattern Matching with Matchable |
|
|
105 | (2) |
|
Values, Variables, and Types in Matches |
|
|
107 | (4) |
|
|
111 | (3) |
|
Pattern Matching on Repeated Parameters |
|
|
114 | (1) |
|
|
114 | (3) |
|
|
116 | (1) |
|
|
117 | (1) |
|
Matching on Case Classes and Enums |
|
|
117 | (3) |
|
Matching on Regular Expressions |
|
|
120 | (1) |
|
Matching on Interpolated Strings |
|
|
121 | (1) |
|
Sealed Hierarchies and Exhaustive Matches |
|
|
121 | (1) |
|
Chaining Match Expressions |
|
|
122 | (1) |
|
Pattern Matching Outside Match Expressions |
|
|
122 | (2) |
|
Problems in Pattern Bindings |
|
|
124 | (1) |
|
Pattern Matching as Filtering in for Comprehensions |
|
|
125 | (1) |
|
Pattern Matching and Erasure |
|
|
126 | (1) |
|
|
127 | (8) |
|
|
127 | (2) |
|
Alternatives to Option Return Values |
|
|
129 | (3) |
|
|
132 | (2) |
|
|
134 | (1) |
|
|
135 | (2) |
|
5 Abstracting Over Context: Type Classes And Extension Methods |
|
|
137 | (28) |
|
|
138 | (1) |
|
|
139 | (3) |
|
Build Your Own String Interpolator |
|
|
142 | (1) |
|
|
142 | (12) |
|
|
143 | (7) |
|
|
150 | (2) |
|
|
152 | (2) |
|
Scala 3 Implicit Conversions |
|
|
154 | (4) |
|
|
158 | (1) |
|
|
159 | (2) |
|
Givens Scoping and Pattern Matching |
|
|
161 | (1) |
|
Resolution Rules for Givens and Extension Methods |
|
|
162 | (1) |
|
|
163 | (1) |
|
|
163 | (2) |
|
6 Abstracting Over Context: Using Clauses |
|
|
165 | (20) |
|
|
165 | (2) |
|
|
167 | (4) |
|
|
171 | (1) |
|
|
172 | (3) |
|
Constraining Allowed Instances |
|
|
175 | (4) |
|
|
178 | (1) |
|
Working Around Type Erasure with Using Clauses |
|
|
179 | (1) |
|
|
180 | (2) |
|
|
182 | (1) |
|
|
183 | (2) |
|
7 Functional Programming In Scala |
|
|
185 | (40) |
|
What Is Functional Programming? |
|
|
185 | (5) |
|
|
186 | (1) |
|
|
187 | (3) |
|
Functional Programming in Scala |
|
|
190 | (3) |
|
Anonymous Functions, Lambdas, and Closures |
|
|
190 | (3) |
|
Purity Inside Versus Outside |
|
|
193 | (1) |
|
|
193 | (1) |
|
Tail Calls and Tail-Call Optimization |
|
|
194 | (1) |
|
Partially Applied Functions Versus Partial Functions |
|
|
195 | (1) |
|
Currying and Uncurrying Functions |
|
|
196 | (1) |
|
Tupled and Untupled Functions |
|
|
197 | (1) |
|
Partial Functions Versus Functions Returning Options |
|
|
198 | (1) |
|
Functional Data Structures |
|
|
199 | (5) |
|
|
200 | (2) |
|
|
202 | (1) |
|
|
203 | (1) |
|
Traversing, Mapping, Filtering, Folding, and Reducing |
|
|
204 | (15) |
|
|
204 | (1) |
|
|
205 | (1) |
|
|
206 | (2) |
|
|
208 | (2) |
|
|
210 | (5) |
|
Left Versus Right Folding |
|
|
215 | (4) |
|
Combinators: Software's Best Component Abstractions |
|
|
219 | (3) |
|
What About Making Copies? |
|
|
222 | (2) |
|
|
224 | (1) |
|
8 For Comprehensions In Depth |
|
|
225 | (20) |
|
Recap: The Elements of for Comprehensions |
|
|
225 | (2) |
|
For Comprehensions: Under the Hood |
|
|
227 | (3) |
|
Translation Rules of for Comprehensions |
|
|
230 | (2) |
|
Options and Container Types |
|
|
232 | (11) |
|
|
232 | (4) |
|
Either: An Alternative to Option |
|
|
236 | (3) |
|
|
239 | (2) |
|
Validated from the Cats Library |
|
|
241 | (2) |
|
|
243 | (2) |
|
9 Object-Oriented Programming In Scala |
|
|
245 | (26) |
|
Class and Object Basics: Review |
|
|
246 | (1) |
|
|
247 | (5) |
|
Classes Open for Extension |
|
|
250 | (1) |
|
Overriding Methods? The Template Method Pattern |
|
|
251 | (1) |
|
Reference Versus Value Types |
|
|
252 | (1) |
|
Opaque Types and Value Classes |
|
|
253 | (8) |
|
|
255 | (3) |
|
|
258 | (3) |
|
|
261 | (1) |
|
|
262 | (1) |
|
Calling Supertype Constructors |
|
|
263 | (1) |
|
|
263 | (2) |
|
Good Object-Oriented Design: A Digression |
|
|
265 | (2) |
|
|
267 | (3) |
|
The Uniform Access Principle |
|
|
268 | (2) |
|
|
270 | (1) |
|
|
270 | (1) |
|
|
271 | (14) |
|
|
271 | (4) |
|
|
275 | (4) |
|
Union and Intersection Types |
|
|
279 | (2) |
|
|
281 | (1) |
|
Using Commas Instead of with |
|
|
282 | (1) |
|
|
282 | (2) |
|
Should That Type Be a Class or Trait? |
|
|
284 | (1) |
|
|
284 | (1) |
|
11 Variance Behavior And Equality |
|
|
285 | (16) |
|
Parameterized Types: Variance Under Inheritance |
|
|
285 | (7) |
|
|
286 | (4) |
|
Variance of Mutable Types |
|
|
290 | (2) |
|
Improper Variance of Java Arrays |
|
|
292 | (1) |
|
|
292 | (3) |
|
|
293 | (1) |
|
|
293 | (1) |
|
|
294 | (1) |
|
Array Equality and the sameElements Method |
|
|
294 | (1) |
|
|
295 | (1) |
|
|
296 | (3) |
|
Case Objects and hashCode |
|
|
299 | (1) |
|
|
300 | (1) |
|
12 Instance Initialization And Method Resolution |
|
|
301 | (10) |
|
Linearization of a Type Hierarchy |
|
|
301 | (4) |
|
Initializing Abstract Fields |
|
|
305 | (2) |
|
Overriding Concrete Fields |
|
|
307 | (1) |
|
Abstract Type Members and Concrete Type Aliases |
|
|
308 | (1) |
|
|
309 | (2) |
|
13 The Scala Type Hierarchy |
|
|
311 | (14) |
|
Much Ado About Nothing (and Null) |
|
|
312 | (3) |
|
|
315 | (1) |
|
Products, Case Classes, Tuples, and Functions |
|
|
316 | (3) |
|
Tuples and the Tuple Trait |
|
|
317 | (2) |
|
|
319 | (4) |
|
|
319 | (2) |
|
|
321 | (1) |
|
Condition Checking Methods |
|
|
321 | (1) |
|
|
322 | (1) |
|
|
323 | (1) |
|
|
323 | (2) |
|
14 The Scala Collections Library |
|
|
325 | (16) |
|
Different Groups of Collections |
|
|
326 | (1) |
|
Abstractions with Multiple Implementations |
|
|
326 | (7) |
|
The scala.collection.immutable Package |
|
|
327 | (2) |
|
The scala.collection.mutable Package |
|
|
329 | (2) |
|
The scalaxollection Package |
|
|
331 | (2) |
|
The scala.collection.concurrent Package |
|
|
333 | (1) |
|
The scalaxollection.convert Package |
|
|
333 | (1) |
|
The scalaxollection.generic Package |
|
|
333 | (1) |
|
Construction of Instances |
|
|
333 | (2) |
|
|
335 | (1) |
|
|
336 | (1) |
|
|
337 | (1) |
|
Nonstrict Collections: Views |
|
|
338 | (1) |
|
|
339 | (2) |
|
|
341 | (6) |
|
Public Visibility: The Default |
|
|
342 | (1) |
|
|
342 | (1) |
|
|
343 | (1) |
|
|
343 | (1) |
|
Scoped Private and Protected Visibility |
|
|
343 | (2) |
|
|
345 | (2) |
|
16 Scala's Type System, Part 1 |
|
|
347 | (22) |
|
|
347 | (1) |
|
Abstract Type Members and Concrete Type Aliases |
|
|
348 | (1) |
|
Comparing Abstract Type Members Versus Parameterized Types |
|
|
348 | (1) |
|
|
349 | (3) |
|
|
349 | (1) |
|
|
350 | (2) |
|
|
352 | (1) |
|
|
352 | (1) |
|
Intersection and Union Types |
|
|
353 | (6) |
|
|
353 | (3) |
|
|
356 | (3) |
|
|
359 | (3) |
|
|
362 | (5) |
|
|
367 | (1) |
|
Existential Types (Obsolete) |
|
|
367 | (1) |
|
|
368 | (1) |
|
17 Scala's Type System, Part II |
|
|
369 | (28) |
|
|
369 | (2) |
|
Dependently Typed Methods |
|
|
371 | (2) |
|
Dependent Method and Dependent Function Types |
|
|
373 | (1) |
|
|
374 | (6) |
|
|
380 | (2) |
|
|
380 | (1) |
|
|
381 | (1) |
|
|
381 | (1) |
|
|
382 | (3) |
|
|
385 | (1) |
|
|
386 | (1) |
|
Self-Recursive Types: F-Bounded Polymorphism |
|
|
387 | (1) |
|
|
388 | (3) |
|
|
391 | (2) |
|
|
393 | (1) |
|
Type Wildcard Versus Placeholder |
|
|
394 | (1) |
|
|
395 | (2) |
|
18 Advanced Functional Programming |
|
|
397 | (14) |
|
|
397 | (3) |
|
Sum Types Versus Product Types |
|
|
397 | (2) |
|
Properties of Algebraic Data Types |
|
|
399 | (1) |
|
Final Thoughts on Algebraic Data Types |
|
|
400 | (1) |
|
|
400 | (9) |
|
|
401 | (1) |
|
|
402 | (2) |
|
|
404 | (4) |
|
The Semigroup and Monoid Categories |
|
|
408 | (1) |
|
|
409 | (2) |
|
|
411 | (18) |
|
The scala.sys.process Package |
|
|
411 | (2) |
|
|
413 | (3) |
|
Robust, Scalable Concurrency with Actors |
|
|
416 | (10) |
|
|
417 | (8) |
|
|
425 | (1) |
|
|
426 | (1) |
|
|
427 | (2) |
|
20 Dynamic Invocation In Scala |
|
|
429 | (10) |
|
Structural Types Revisited |
|
|
429 | (3) |
|
A Motivating Example: ActiveRecord in Ruby on Rails |
|
|
432 | (1) |
|
Dynamic Invocation with the Dynamic Trait |
|
|
433 | (4) |
|
|
437 | (1) |
|
|
437 | (2) |
|
21 Domain-Specific Languages In Scala |
|
|
439 | (12) |
|
|
440 | (6) |
|
External DSLs with Parser Combinators |
|
|
446 | (3) |
|
|
447 | (1) |
|
|
447 | (2) |
|
Internal Versus External DSLs: Final Thoughts |
|
|
449 | (1) |
|
|
450 | (1) |
|
22 Scala Tools And Libraries |
|
|
451 | (16) |
|
|
451 | (1) |
|
Command-Line Interface Tools |
|
|
452 | (5) |
|
|
453 | (1) |
|
Managing Java JDKs with Coursier |
|
|
453 | (1) |
|
The scalac Command-Line Tool |
|
|
454 | (1) |
|
The scala Command-Line Tool |
|
|
455 | (1) |
|
The scaladoc Command-Line Tool |
|
|
456 | (1) |
|
Other Scala Command-Line Tools |
|
|
456 | (1) |
|
|
457 | (1) |
|
Integration with IDEs and Text Editors |
|
|
457 | (1) |
|
Using Notebook Environments with Scala |
|
|
457 | (1) |
|
|
458 | (1) |
|
Scala for Big Data: Apache Spark |
|
|
459 | (2) |
|
|
461 | (1) |
|
|
462 | (1) |
|
Java and Scala Interoperability |
|
|
462 | (3) |
|
Using Java Identifiers in Scala Code |
|
|
462 | (1) |
|
Scala Identifiers in Java Code |
|
|
463 | (1) |
|
Java Generics and Scala Parameterized Types |
|
|
463 | (1) |
|
Conversions Between Scala and Java Collections |
|
|
464 | (1) |
|
Java Lambdas Versus Scala Functions |
|
|
464 | (1) |
|
Annotations for JavaBean Properties and Other Purposes |
|
|
464 | (1) |
|
|
465 | (2) |
|
|
467 | (18) |
|
Recap of What We Already Know |
|
|
467 | (1) |
|
|
468 | (1) |
|
|
469 | (1) |
|
|
470 | (4) |
|
|
470 | (1) |
|
|
471 | (1) |
|
|
472 | (2) |
|
Better Design with Design by Contract |
|
|
474 | (3) |
|
The Parthenon Architecture |
|
|
477 | (6) |
|
|
483 | (2) |
|
24 Metaprogramming: Macros And Reflection |
|
|
485 | (18) |
|
Scala Compile Time Reflection |
|
|
487 | (1) |
|
|
488 | (2) |
|
|
490 | (1) |
|
Type Class Derivation: Implementation Details |
|
|
491 | (1) |
|
|
491 | (11) |
|
|
491 | (5) |
|
|
496 | (4) |
|
|
500 | (2) |
|
Wrapping Up and Looking Ahead |
|
|
502 | (1) |
A Significant Indentation Versus Braces Syntax |
|
503 | (4) |
Bibliography |
|
507 | (2) |
Index |
|
509 | |