Cover image for Programming Scala

Book description

Learn how to be more productive with Scala, a new multi-paradigm language for the Java Virtual Machine (JVM) that integrates features of both object-oriented and functional programming. With this book, you'll discover why Scala is ideal for highly scalable, component-based applications that support concurrency and distribution. Programming Scala clearly explains the advantages of Scala as a JVM language. You'll learn how to leverage the wealth of Java class libraries to meet the practical needs of enterprise and Internet projects more easily. Packed with code examples, this book provides useful information on Scala's command-line tools, third-party tools, libraries, and available language-aware plugins for editors and IDEs.

  • Learn how Scala's succinct and flexible code helps you program faster

  • Discover the notable improvements Scala offers over Java's object model

  • Get a concise overview of functional programming, and learn how Scala's support for it offers a better approach to concurrency

  • Know how to use mixin composition with traits, pattern matching, concurrency with Actors, and other essential features

  • Take advantage of Scala's built-in support for XML

  • Learn how to develop domain-specific languages

  • Understand the basics for designing test-driven Scala applications

Table of Contents

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