ebook img

Scala for Java Developers PDF

241 Pages·2018·2.703 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Scala for Java Developers

Scala for Java Developers A Practical Primer Toby Weston Scala for Java Developers: A Practical Primer Toby Weston London, United Kingdom ISBN-13 (pbk): 978-1-4842-3107-4 ISBN-13 (electronic): 978-1-4842-3108-1 https://doi.org/10.1007/978-1-4842-3108-1 Library of Congress Control Number: 2017963118 Copyright © 2018 by Toby Weston Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484231074. For more detailed information, please visit http://www.apress.com/source-code. Contents Preface ...................................................................................................................xix Part I: Scala Tour ............................................................................................1 Chapter 1: The Scala Language ................................................................................3 As a Functional Programming Language ��������������������������������������������������������������������������������������3 The Past ����������������������������������������������������������������������������������������������������������������������������������������4 The Future �������������������������������������������������������������������������������������������������������������������������������������5 Chapter 2: Installing Scala .......................................................................................7 Getting Started ������������������������������������������������������������������������������������������������������������������������������7 The Scala Interpreter ��������������������������������������������������������������������������������������������������������������������7 Scala Scripts ���������������������������������������������������������������������������������������������������������������������������������8 scalac ��������������������������������������������������������������������������������������������������������������������������������������������9 Chapter 3: Some Basic Syntax................................................................................11 Defining Values and Variables �����������������������������������������������������������������������������������������������������11 Defining Functions ����������������������������������������������������������������������������������������������������������������������12 Operator Overloading and Infix Notation �������������������������������������������������������������������������������������14 Collections ����������������������������������������������������������������������������������������������������������������������������������15 Tuples ������������������������������������������������������������������������������������������������������������������������������������������17 Java Interoperability �������������������������������������������������������������������������������������������������������������������18 Primitive Types ����������������������������������������������������������������������������������������������������������������������������19 Chapter 4: Scala’s Class Hierarchy .........................................................................21 AnyVal �����������������������������������������������������������������������������������������������������������������������������������������22 Unit ����������������������������������������������������������������������������������������������������������������������������������������������23 AnyRef �����������������������������������������������������������������������������������������������������������������������������������������24 Bottom Types ������������������������������������������������������������������������������������������������������������������������������25 Chapter 5: ScalaDoc ...............................................................................................29 Chapter 6: Language Features ................................................................................33 Working with Source Code ����������������������������������������������������������������������������������������������������������33 Working with Methods ����������������������������������������������������������������������������������������������������������������34 Functional Programming �������������������������������������������������������������������������������������������������������������36 Chapter 7: Summary ...............................................................................................37 Part II: Key Syntactical Differences ..............................................................39 Chapter 8: Classes and Fields ................................................................................43 Creating Classes �������������������������������������������������������������������������������������������������������������������������43 Derived Setters and Getters ��������������������������������������������������������������������������������������������������������44 Redefining Setters and Getters ���������������������������������������������������������������������������������������������������48 Summary�������������������������������������������������������������������������������������������������������������������������������������52 Chapter 9: Classes and Objects ..............................................................................53 Classes Without Constructor Arguments �������������������������������������������������������������������������������������53 Additional Constructors ���������������������������������������������������������������������������������������������������������������55 Using Default Values ��������������������������������������������������������������������������������������������������������������57 Singleton Objects ������������������������������������������������������������������������������������������������������������������������58 Companion Objects ���������������������������������������������������������������������������������������������������������������������61 Other Uses for Companion Objects ����������������������������������������������������������������������������������������63 Chapter 10: Classes and Functions ........................................................................67 Anonymous Functions �����������������������������������������������������������������������������������������������������������������67 Anonymous Classes ��������������������������������������������������������������������������������������������������������������������68 First-class Functions �������������������������������������������������������������������������������������������������������������������69 Passing in Functions �������������������������������������������������������������������������������������������������������������70 Returning Functions ��������������������������������������������������������������������������������������������������������������71 Storing Functions ������������������������������������������������������������������������������������������������������������������72 Function Types ����������������������������������������������������������������������������������������������������������������������������74 Functions vs� Methods ����������������������������������������������������������������������������������������������������������������74 Lambdas vs� Closures �����������������������������������������������������������������������������������������������������������������75 Chapter 11: Inheritance ..........................................................................................79 Subtype Inheritance ��������������������������������������������������������������������������������������������������������������������79 Anonymous Classes ��������������������������������������������������������������������������������������������������������������������82 Interfaces/Traits ��������������������������������������������������������������������������������������������������������������������������84 Methods on Traits ������������������������������������������������������������������������������������������������������������������87 Converting Anonymous Classes to Lambdas �������������������������������������������������������������������������94 Concrete Fields on Traits �������������������������������������������������������������������������������������������������������95 Abstract Fields on Traits ��������������������������������������������������������������������������������������������������������96 Abstract Classes �������������������������������������������������������������������������������������������������������������������������97 Polymorphism �����������������������������������������������������������������������������������������������������������������������������98 Traits vs� Abstract Classes �����������������������������������������������������������������������������������������������������99 Deciding Between the Options ��������������������������������������������������������������������������������������������������103 Chapter 12: Control Structures .............................................................................105 Conditionals ������������������������������������������������������������������������������������������������������������������������������105 Ifs and Ternaries ������������������������������������������������������������������������������������������������������������������105 Switch Statements ��������������������������������������������������������������������������������������������������������������108 Looping Structures: do, while and for ���������������������������������������������������������������������������������������113 Breaking Control Flow (break and continue) �����������������������������������������������������������������������������116 Exceptions ���������������������������������������������������������������������������������������������������������������������������������117 Chapter 13: Generics ............................................................................................121 Parametric Polymorphism ���������������������������������������������������������������������������������������������������������121 Class Generics ���������������������������������������������������������������������������������������������������������������������122 Method Generics �����������������������������������������������������������������������������������������������������������������123 Stack Example ���������������������������������������������������������������������������������������������������������������������123 Bounded Classes �����������������������������������������������������������������������������������������������������������������������126 Upper Bounds (<U extends T>) �������������������������������������������������������������������������������������������127 Lower Bounds (<U super T>) ����������������������������������������������������������������������������������������������128 Wildcard Bounds (<? extends T, <? super T>) ��������������������������������������������������������������������134 Multiple Bounds �������������������������������������������������������������������������������������������������������������������136 Variance ������������������������������������������������������������������������������������������������������������������������������������137 Invariance ����������������������������������������������������������������������������������������������������������������������������138 Covariance ���������������������������������������������������������������������������������������������������������������������������139 Contravariance ��������������������������������������������������������������������������������������������������������������������139 Variance Summary ��������������������������������������������������������������������������������������������������������������139 Part III: Beyond Java to Scala .....................................................................141 Chapter 14: Faking Function Calls ........................................................................143 The apply Method ���������������������������������������������������������������������������������������������������������������������143 The update Method ������������������������������������������������������������������������������������������������������������������145 Multiple update Methods �����������������������������������������������������������������������������������������������������146 Multiple Arguments to update ���������������������������������������������������������������������������������������������147 Summary�����������������������������������������������������������������������������������������������������������������������������������148 Chapter 15: Faking Language Constructs .............................................................149 Curly Braces (and Function Literals) �����������������������������������������������������������������������������������������149 Higher-Order Functions �������������������������������������������������������������������������������������������������������150 Higher-Order Functions with Curly Braces ��������������������������������������������������������������������������152 Call-by-Name�����������������������������������������������������������������������������������������������������������������������152 Currying ������������������������������������������������������������������������������������������������������������������������������������154 Scala Support for Curried Functions �����������������������������������������������������������������������������������157 Summary�����������������������������������������������������������������������������������������������������������������������������������158 Chapter 16: Pattern Matching ..............................................................................159 Switching ����������������������������������������������������������������������������������������������������������������������������������159 Patterns �������������������������������������������������������������������������������������������������������������������������������������160 Literal Matches �������������������������������������������������������������������������������������������������������������������������161 Constructor Matches �����������������������������������������������������������������������������������������������������������������162 Type Query ��������������������������������������������������������������������������������������������������������������������������������165 Deconstruction Matches and unapply ���������������������������������������������������������������������������������������166 Why Write Your Own Extractors? �����������������������������������������������������������������������������������������168 Guard Conditions �����������������������������������������������������������������������������������������������������������������������169 Chapter 17: Map and FlatMap ..............................................................................171 Mapping Functions �������������������������������������������������������������������������������������������������������������������171 It’s Like foreach �������������������������������������������������������������������������������������������������������������������172 FlatMap �������������������������������������������������������������������������������������������������������������������������������������173 Not Just for Collections �������������������������������������������������������������������������������������������������������������176 Chapter 18: Monads .............................................................................................177 Basic Definition �������������������������������������������������������������������������������������������������������������������������177 Option ����������������������������������������������������������������������������������������������������������������������������������������177 The map Function ����������������������������������������������������������������������������������������������������������������178 Option�get ����������������������������������������������������������������������������������������������������������������������������181 Option�getOrElse ������������������������������������������������������������������������������������������������������������������182 Monadically Processing Option �������������������������������������������������������������������������������������������182 The Option�flatMap Function �����������������������������������������������������������������������������������������������183 More Formal Definition �������������������������������������������������������������������������������������������������������������184 Summary�����������������������������������������������������������������������������������������������������������������������������������186 Chapter 19: For Comprehensions .........................................................................187 Where We Left Off ���������������������������������������������������������������������������������������������������������������������187 Using Null Checks ���������������������������������������������������������������������������������������������������������������������188 Using flatMap with Option ���������������������������������������������������������������������������������������������������������189 How For Comprehensions Work ������������������������������������������������������������������������������������������������191 Finally, Using a For Comprehension for Shipping Labels ����������������������������������������������������������194 Summary�����������������������������������������������������������������������������������������������������������������������������������195 Part IV: Adopting Scala in Java Teams .......................................................197 Chapter 20: Adopting Scala ..................................................................................199 Avoid Not Enough ����������������������������������������������������������������������������������������������������������������������199 Don’t Do Too Much ��������������������������������������������������������������������������������������������������������������������199 Purely Functional FTW? ������������������������������������������������������������������������������������������������������������200 Chapter 21: What to Expect ..................................................................................201 The Learning Curve �������������������������������������������������������������������������������������������������������������������201 The Learning Continuum �����������������������������������������������������������������������������������������������������������203 Goals ������������������������������������������������������������������������������������������������������������������������������������204 Chapter 22: Tips....................................................................................................205 Be Clear�������������������������������������������������������������������������������������������������������������������������������������205 Get Guidance �����������������������������������������������������������������������������������������������������������������������������206 Have a Plan �������������������������������������������������������������������������������������������������������������������������������206 Chapter 23: Convert Your Codebase .....................................................................209 Chapter 24: Manage Your Codebase .....................................................................211 Conventions ������������������������������������������������������������������������������������������������������������������������������211 What to Avoid ����������������������������������������������������������������������������������������������������������������������������212 Other Challenges �����������������������������������������������������������������������������������������������������������������������213 Appendix A: Code Listings ....................................................................................215 Inheritance ��������������������������������������������������������������������������������������������������������������������������������215 Subtype Inheritance in Java ������������������������������������������������������������������������������������������������215 Anonymous Classes in Java ������������������������������������������������������������������������������������������������217 Subtype Inheritance in Scala �����������������������������������������������������������������������������������������������218 Anonymous Classes in Scala ����������������������������������������������������������������������������������������������������219 Generics ������������������������������������������������������������������������������������������������������������������������������������219 Lower Bounds in Java ���������������������������������������������������������������������������������������������������������219 Multiple Bounds in Java ������������������������������������������������������������������������������������������������������223 Lower Bounds in Scala ��������������������������������������������������������������������������������������������������������224 Multiple Bounds in Scala �����������������������������������������������������������������������������������������������������226 Pattern Matching ����������������������������������������������������������������������������������������������������������������������227 Constructor Matches �����������������������������������������������������������������������������������������������������������227 Deconstruction Matches and Unapply ���������������������������������������������������������������������������������229 Map �������������������������������������������������������������������������������������������������������������������������������������������229 Mapping Functions ��������������������������������������������������������������������������������������������������������������229 FlatMap ��������������������������������������������������������������������������������������������������������������������������������231 Appendix B: Syntax Cheat Sheet...........................................................................233 Index .....................................................................................................................241 Preface Audience This book is for Java developers looking to transition to programming in Scala. The Structure of the Book The book is split into four parts: a tour of Scala, a comparison between Java and Scala, a closer look at Scala-specific features and functional programming idioms, and finally a discussion about adopting Scala into existing Java teams. In Part I, we’re going to take a high-level tour of Scala. You’ll get a feel for the language’s constructs and how Scala is similar in a lot of ways to Java, yet very different in others. We’ll take a look at installing Scala and using the interactive interpreter and we’ll go through some basic syntax examples. Part II talks about key differences between Java and Scala. We’ll look at what’s missing in Scala compared to Java, vice versa, and how concepts translate from one language to another. Then in Part III, we’ll talk about some of the language features that Scala offers that aren’t found in Java. This part also talks a little about functional programming idioms. Finally, we’ll talk about adopting Scala into legacy Java projects and teams. It’s not always an easy transition, so we’ll look at why you would want to, and some of the challenges you might face. xix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.