Scala for the Impatient
Cay S. Horstmann
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 3823419
For sales outside the United States, please contact:
International Sales
Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Horstmann, Cay S., 1959
Scala for the impatient / Cay S. Horstmann.
p. cm.
Includes index.
ISBN 978-0-321-77409-5 (pbk. : alk. paper)ISBN 0-321-77409-4 (pbk. :
alk. paper) 1. Scala (Computer program language) 2. Programming
languages (Electronic computers) 3. Computer programming. I. Title.
QA76.73.S28H67 2012
005.13'3dc23
2011052136
Copyright 2012 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
ISBN-13: 978-0-321-77409-5
ISBN-10: 0-321-77409-4
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.
Second printing, June 2013
To my wife, who made writing this book possible,
and to my children, who made it necessary.
Contents
Foreword
When I met Cay Horstmann some years ago he told me that Scala needed a better introductory book. My own book had come out a little bit earlier, so of course I had to ask him what he thought was wrong with it. He responded that it was great but too long; his students would not have the patience to read through the eight hundred pages of Programming in Scala. I conceded that he had a point. And he set out to correct the situation by writing Scala for the Impatient.
I am very happy that his book has finally arrived because it really delivers on what the title says. It gives an eminently practical introduction to Scala, explains whats particular about it, how it differs from Java, how to overcome some common hurdles to learning it, and how to write good Scala code.
Scala is a highly expressive and flexible language. It lets library writers use highly sophisticated abstractions, so that library users can express themselves simply and intuitively. Therefore, depending on what kind of code you look at, it might seem very simple or very complex.
A year ago, I tried to provide some clarification by defining a set of levels for Scala and its standard library. There were three levels each for application programmers and for library designers. The junior levels could be learned quickly and would be sufficient to program productively. Intermediate levels would make programs more concise and more functional and would make libraries more flexible to use. The highest levels were for experts solving specialized tasks. At the time I wrote:
I hope this will help newcomers to the language decide in what order to pick subjects to learn, and that it will give some advice to teachers and book authors in what order to present the material.
Cays book is the first to have systematically applied this idea. Every chapter is tagged with a level that tells you how easy or hard it is and whether its oriented towards library writers or application programmers.
As you would expect, the first chapters give a fast-paced introduction to the basic Scala capabilities. But the book does not stop there. It also covers many of the more senior concepts and finally progresses to very advanced material which is not commonly covered in a language introduction, such as how to write parser combinators or make use of delimited continuations. The level tags serve as a guideline for what to pick up when. And Cay manages admirably to make even the most advanced concepts simple to understand.
I liked the concept of Scala for the Impatient so much that I asked Cay and his editor, Greg Doench, whether we could get the first part of the book as a free download on the Typesafe web site. They have gracefully agreed to my request, and I would like to thank them for that. That way, everybody can quickly access what I believe is currently the best compact introduction to Scala.
Martin Odersky
January 2012
Preface
The evolution of Java and C++ has slowed down considerably, and programmers who are eager to use more modern language features are looking elsewhere. Scala is an attractive choice; in fact, I think it is by far the most attractive choice for programmers who want to move beyond Java or C++. Scala has a concise syntax that is refreshing after the Java boilerplate. It runs on the Java virtual machine, providing access to a huge set of libraries and tools. It embraces the functional programming style without abandoning object orientation, giving you an incremental learning path to a new paradigm. The Scala interpreter lets you run quick experiments, which makes learning Scala very enjoyable. Last but not least, Scala is statically typed, enabling the compiler to find errors, so that you dont waste time finding themor notlater in the running program.
I wrote this book for impatient readers who want to start programming in Scala right away. I assume you know Java, C#, or C++, and I dont bore you with explaining variables, loops, or classes. I dont exhaustively list all the features of the language, I dont lecture you about the superiority of one paradigm over another, and I dont make you suffer through long and contrived examples. Instead, you will get the information that you need in compact chunks that you can read and review as needed.
Scala is a big language, but you can use it effectively without knowing all of its details intimately. Martin Odersky, the creator of Scala, has identified levels of expertise for application programmers and library designersas shown in the following table.
For each chapter (and occasionally for individual sections), I indicate the experience level required. The chapters progress through levels