Scala in Depth
Joshua D. Suereth & Foreword by: Martin Odersky
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email:
orders@manning.com2012 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
| Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. |
| Manning Publications Co.20 Baldwin RoadPO Box 261Shelter Island, NY 11964 | Development editor : Katharine OsborneTechnical proofreader: Justin Copyeditors: Linda Kern, Benjamin Berg Proofreader: Elizabeth Martin Typesetter: Dottie Marsico Cover designer: Marija Tudor |
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 MAL 17 16 15 14 13 12
Brief Table of Contents
Table of Contents
Foreword
Joshua Suereth is one of the most complete programmers I know. Familiar with a whole gamut of programming languages and techniques, he is an expert in high-performance systems, build tools, type theory, and many other areas. He is also a gifted teacher, and all that combined is what makes Scala in Depth special.
This book provides in-depth coverage of several of the more intricate areas of Scala, including advanced aspects of its type system, implicits, composition techniques with traits, collections, actors, functional categories. But this is not a dry recollection of language and library concepts. The book is full of practical advice on how to apply these lesser known parts of Scala in useful ways, and what the best practices are. The explanations and examples demonstrate Joshuas great experience constructing large-scale systems in Scala.
Scala in Depth is not a beginners introduction; it should primarily appeal to competent Scala programmers who want to become experts. The techniques that are taught are handy for constructing flexible and type-safe library abstractions. Many of these techniques were folklore until now; they have been, for the first time, written-up here.
I am particularly happy about one other thing: The book fills a gap in that it explains key parts of the formal Scala specification to programmers who are not language lawyers. Scala is one of few languages that actually has a specification. That specification consists mainly of definitions written in highly stylized prose and mathematical formulas; so its not everybodys piece of cake. Joshuas book manages to be both authorative and understandable as it explains these concepts.
MARTIN ODERSKY
CREATOR OF SCALA
HEAD OF PROGRAMMING
RESEARCH GROUP, EPFL
Preface
In fall 2010 Michael Stephens from Manning contacted me about writing a Scala Book. I was working for a small virtualization/security startup where I had been learning Scala and applying it to our codebase. During that first conversation Michael and I discussed the Scala ecosystem and what kind of a book would best serve the community.
I believed Scala needed a practical Scala book to help guide those new to the language. Scala is a beautiful language, but it brings many new concepts to the table. I had watched as the community slowly discovered best practices and a code style that was wholly Scala. But I wasnt sure whether I was the right person to write such a book. When it came down to it, I was passionate about the topic, had enough free time to do the research, and had the support of the magnates of the community to help achieve what you are reading todayso I decided to go ahead.
Ive learned a lot during the writing process. One reason it took so long was the evolving nature of Scala and the emergence of new best practices. Another reason was that I realized my own knowledge was woefully inadequate in some areas of Scala. To all aspiring authors out there, I will tell you that writing a book makes you an expert. You may think you are one before you start, but true expertise grows from the blood, sweat, and tears of teaching, of trying to convey complex concepts to your readers with clarity.
Working on this book was a journey that I never could have completed without a very supportive and loving wife, a great publisher, and an amazing community of Scala developers and readers willing to read my manuscript in various stages, point out my typos and misspellings, and offer advice on how to make Scala in Depth a much better book than I could have achieved alone.
Acknowledgments
Many people helped get this book off the ground and into print. While Im going to try to list them all, Im sure Ill miss a few as there were just too many for my tiny brain to remember. This book showed me that I have a lot of high quality friends, coworkers, and family.
The biggest thank you is for my wife and children, who had to deal with a husband/father who was constantly hiding in a corner, writing, when he should have been helping out. Theres no way an author can write a book without the support of immediate family, and mine was no exception.
Next, Id like to thank Manning Publications and all the work the staff did to ensure I became a real author. Not only did they review and lay out the book, they also helped improve my technical writing skills for clear communication. I cant give enough thanks to the whole team, but Id especially like to thank Katherine Osborne for putting up with my missed deadlines, Pennsylvania-Dutch sentence structures, and overall poor spelling. Katherine was instrumental to the voice of this book, and those whove been reading the MEAPs will notice the improvement.
The next group that deserves thanks are the Scala experts and nonexperts who helped me improve my technical material and descriptions. Tim Perret was authoring Lift in Action for Manning around the same time I was writing Scala in Depth. Discussions with Tim were both encouraging and motivating. Unfortunately for me, he finished first. Justin Wick was a reviewer and collaborator on a lot of the content, and definitely helped me reach a wider audience than I had initially attempted to attract. He also reviewed the final manuscript and code one last time, just before the book went into production. Adriaan Moors, as usual, pointed out all my mistakes when discussing the type system and implicit resolution and helped make the discussions both practical and correct. Eric Weinberg was an old coworker of mine who helped provide guidance for reaching non-Scala developers in the book. Viktor Klang reviewed the Actors chapter (and the whole book) and offered improvements. Thank you also to Martin Odersky for his endorsement and kind words on the final product that you will read in the foreword, Josh Cough for being a guy I can bounce ideas off when needed, and Peter Simanyi for an email with a very detailed, thorough, complete, and awesome review of the entire book.