Programming Scala

Dean Wampler

Object Mentor, Inc.

Alex Payne

Twitter, Inc.

This work has been released under the Creative Commons Attribution-Noncommercial.

Abstract

Programming Scala introduces an exciting new language that offers all the benefits of a modern object model, functional programming, and an advanced type system. Packed with code examples, this comprehensive book teaches you how to be productive with Scala quickly, and explains what makes this language ideal for today's highly scalable, component-based applications that support concurrency and distribution. You'll also learn the advantages that Scala offers as a language for the Java Virtual Machine.

Learn more at programmingscala.com or at the book's catalog page.


Preface
Conventions Used in This Book
Using Code Examples
Getting the Code Examples
Safari® Books Online
How to Contact Us
1. Zero to Sixty: Introducing Scala
Why Scala?
If You Are a Java Programmer…
If You Are a Ruby, Python, etc. Programmer…
Introducing Scala
The Seductions of Scala
Installing Scala
For More Information
A Taste of Scala
A Taste of Concurrency
Recap and What’s Next
2. Type Less, Do More
In This Chapter
Semicolons
Variable Declarations
Method Declarations
Method Default and Named Arguments (Scala Version 2.8)
Nesting Method Definitions
Inferring Type Information
Literals
Integer Literals
Floating Point Literals
Boolean Literals
Character Literals
String Literals
Symbol Literals
Tuples
Option, Some, and None: Avoiding nulls
Organizing Code in Files and Namespaces
Importing Types and Their Members
Imports are Relative
Abstract Types And Parameterized Types
Reserved Words
Recap and What’s Next
3. Rounding Out the Essentials
Operator? Operator?
Syntactic Sugar
Methods Without Parentheses and Dots
Precedence Rules
Domain-Specific Languages
Scala if Statements
Scala for Comprehensions
A Dog-Simple Example
Filtering
Yielding
Expanded Scope
Other Looping Constructs
Scala while Loops
Scala do-while Loops
Generator Expressions
Conditional Operators
Pattern Matching
A Simple Match
Variables in Matches
Matching on Type
Matching on Sequences
Matching on Tuples (and Guards)
Matching on Case Classes
Matching on Regular Expressions
Binding Nested Variables in Case Clauses
Using try, catch, and finally Clauses
Concluding Remarks on Pattern Matching
Enumerations
Recap and What’s Next
4. Traits
Introducing Traits
Traits as Mixins
Stackable Traits
Constructing Traits
Class or Trait?
Recap and What’s Next
5. Basic Object-Oriented Programming in Scala
Class and Object Basics
Parent Classes
Constructors in Scala
Calling Parent Class Constructors
Nested Classes
Visibility Rules
Public Visibility
Protected Visibility
Private Visibility
Scoped Private and Protected Visibility
Final Thoughts on Visibility
Recap and What’s Next
6. Advanced Object-Oriented Programming In Scala
Overriding Members of Classes and Traits
Attempting to Override final Declarations
Overriding Abstract and Concrete Methods
Overriding Abstract and Concrete Fields
Overriding Abstract and Concrete Fields in Traits
Overriding Abstract and Concrete Fields in Classes
Overriding Abstract Types
When Accessor Methods and Fields Are Indistinguishable: The Uniform Access Principle
Companion Objects
Apply
Unapply
Apply and UnapplySeq for Collections
Companion Objects and Java Static Methods
Case Classes
Syntactic Sugar for Binary Operations
The copy Method in Scala Version 2.8
Case Class Inheritance
Equality of Objects
The equals Method
The == and != Methods
The ne and eq Methods
Array Equality and the sameElements Method
Recap and What’s Next
7. The Scala Object System
The Predef Object
Classes and Objects: Where Are the Statics?
Package Objects
Sealed Class Hierarchies
The Scala Type Hierarchy
Linearization of an Object’s Hierarchy
Recap and What’s Next
8. Functional Programming in Scala
What Is Functional Programming?
Functions in Mathematics
Variables that Aren’t
Functional Programming in Scala
Function Literals and Closures
Purity Inside vs. Outside
Recursion
Tail Calls and Tail-Call Optimization
Trampoline for Tail Calls
Functional Data Structures
Lists in Functional Programming
Maps in Functional Programming
Sets in Functional Programming
Other Data Structures in Functional Programming
Traversing, Mapping, Filtering, Folding, and Reducing
Traversal
Mapping
Filtering
Folding and Reducing
Functional Options
Pattern Matching
Partial Functions
Currying
Implicits
Implicit Conversions
Implicit Function Parameters
Final Thoughts on Implicits
Call by Name, Call by Value
Lazy Vals
Recap: Functional Component Abstractions
9. Robust, Scalable Concurrency with Actors
The Problems of Shared, Synchronized State
Actors
Actors in Abstract
Actors in Scala
Sending Messages to Actors
The Mailbox
Actors in Depth
Effective Actors
Traditional Concurrency in Scala: Threading and Events
One-Off Threads
Using java.util.concurrent
Events
Recap and What’s Next
10. Herding XML in Scala
Reading XML
Exploring XML
Looping & Matching XML
Writing XML
A Real-World Example
Recap and What’s Next
11. Domain-Specific Languages in Scala
Internal DSLs
A Payroll Internal DSL
Infix Operator Notation
Implicit Conversions and User Defined Types
Apply Methods
Payroll Rules DSL Implementation
Internal DSLs: Final Thoughts
External DSLs with Parser Combinators
About Parser Combinators
A Payroll External DSL
A Scala Implementation of the External DSL Grammar
Generating Paychecks with the External DSL
Internal vs. External DSLs: Final Thoughts
Recap and What’s Next
12. The Scala Type System
Reflecting on Types
Understanding Parameterized Types
Manifests
Parameterized Methods
Variance Under Inheritance
Variance of Mutable Types
Variance In Scala vs. Java
Implementation Notes
Type Bounds
Upper Type Bounds
Lower Type Bounds
A Closer Look at Lists
Views and View Bounds
Nothing and Null
Understanding Abstract Types
Parameterized Types vs. Abstract Types
Path-Dependent Types
C.this
C.super
path.x
Value Types
Type Designators
Tuples
Parameterized Types
Annotated Types
Compound Types
Infix Types
Function Types
Type Projections
Singleton Types
Self Type Annotations
Structural Types
Existential Types
Infinite Data Structures and Laziness
Recap and What’s Next
13. Application Design
Annotations
Enumerations vs. Pattern Matching
Thoughts On Annotations and Enumerations
Enumerations vs. Case Classes and Pattern Matching
Using Nulls vs. Options
Options and For Comprehensions
Exceptions and the Alternatives
Scalable Abstractions
Fine-Grained Visibility Rules
Mixin Composition
Self-Type Annotations and Abstract Type Members
Effective Design of Traits
Design Patterns
The Visitor Pattern: A Better Alternative
Dependency Injection in Scala: The Cake Pattern
Better Design with Design By Contract
Recap and What’s Next
14. Scala Tools, Libraries and IDE Support
Command Line Tools
scalac Command Line Tool
The scala Command Line Tool
The scalap, javap, and jad Command Line Tools
The scaladoc Command Line Tool
The sbaz Command Line Tool
The fsc Command Line Tool
Build Tools
Integration with IDEs
Eclipse
IntelliJ
NetBeans
Text Editors
Test-Driven Development in Scala
ScalaTest
Specs
ScalaCheck
Other Notable Scala Libraries and Tools
Lift
Scalaz
Scalax
MetaScala
JavaRebel
Miscellaneous Smaller Libraries
Java Interoperability
Java and Scala Generics
Using Scala Functions in Java
JavaBean Properties
AnyVal Types and Java Primitives
Scala Names in Java Code
Java Library Interoperability
AspectJ
The Spring Framework
Terracotta
Hadoop
Recap and What’s Next
Glossary
A. References
Index
You must sign in or register before commenting