| 1 Introduction |
|
1 | (6) |
|
|
|
1 | (1) |
|
|
|
1 | (1) |
|
|
|
2 | (1) |
|
1.4 Java to Scala Quick Comparison |
|
|
3 | (1) |
|
1.5 Is This Book for You? |
|
|
4 | (1) |
|
1.6 Approach Taken by This Book |
|
|
5 | (1) |
|
|
|
5 | (2) |
| 2 Elements of Object Orientation |
|
7 | (8) |
|
|
|
7 | (1) |
|
|
|
7 | (2) |
|
|
|
9 | (2) |
|
2.4 The Move to Object Technology |
|
|
11 | (1) |
|
|
|
12 | (1) |
|
|
|
13 | (1) |
|
|
|
13 | (2) |
| 3 Why Object Orientation? |
|
15 | (6) |
|
|
|
15 | (1) |
|
3.2 The Procedural Approach |
|
|
15 | (2) |
|
3.2.1 A Naked Data Structure |
|
|
15 | (1) |
|
3.2.2 Procedures for the Data Structure |
|
|
16 | (1) |
|
|
|
17 | (1) |
|
3.3 Does Object Orientation Do Better? |
|
|
17 | (3) |
|
3.3.1 Packages Versus Classes |
|
|
17 | (2) |
|
|
|
19 | (1) |
|
|
|
20 | (1) |
| 4 Constructing an Object Oriented System |
|
21 | (12) |
|
|
|
21 | (1) |
|
4.2 The Application: Windscreen Wipe Simulation |
|
|
21 | (1) |
|
|
|
22 | (1) |
|
4.4 Identifying the Objects |
|
|
23 | (1) |
|
4.5 Identifying the Services or Methods |
|
|
24 | (1) |
|
|
|
25 | (1) |
|
4.7 Bringing It All Together |
|
|
26 | (3) |
|
4.8 Where Is the Structure? |
|
|
29 | (2) |
|
|
|
31 | (1) |
|
|
|
32 | (1) |
|
|
|
32 | (1) |
|
|
|
32 | (1) |
| 5 Functional Programming |
|
33 | (6) |
|
|
|
33 | (1) |
|
5.2 What is Functional Programming? |
|
|
33 | (2) |
|
5.3 Advantages to Functional Programming |
|
|
35 | (1) |
|
5.4 Disadvantages of Functional Programming |
|
|
36 | (1) |
|
5.5 Scala and Functional Programming |
|
|
37 | (2) |
| 6 Scala Background |
|
39 | (4) |
|
|
|
39 | (1) |
|
|
|
39 | (2) |
|
6.3 Functional Programming |
|
|
41 | (1) |
|
|
|
42 | (1) |
| 7 A Little Scala |
|
43 | (16) |
|
|
|
43 | (1) |
|
7.2 The Scala Environment |
|
|
43 | (1) |
|
|
|
44 | (1) |
|
|
|
45 | (6) |
|
7.4.1 Selecting a Workspace |
|
|
45 | (2) |
|
|
|
47 | (1) |
|
|
|
47 | (2) |
|
|
|
49 | (2) |
|
7.5 Implementing the Object |
|
|
51 | (1) |
|
7.6 Running the Application |
|
|
52 | (1) |
|
|
|
52 | (1) |
|
7.8 Compiling and Executing Scala |
|
|
53 | (2) |
|
|
|
55 | (2) |
|
7.9.1 Why Have Automatic Memory Management? |
|
|
55 | (1) |
|
7.9.2 Memory Management in Scala |
|
|
56 | (1) |
|
7.9.3 When Is Garbage Collection Performed? |
|
|
56 | (1) |
|
7.9.4 Checking the Available Memory |
|
|
56 | (1) |
|
|
|
57 | (1) |
|
|
|
58 | (1) |
|
|
|
58 | (1) |
| 8 Scala Building Blocks |
|
59 | (4) |
|
|
|
59 | (1) |
|
8.2 Apps and Applications |
|
|
59 | (1) |
|
8.3 The Basics of the Language |
|
|
60 | (3) |
|
|
|
61 | (1) |
|
8.3.2 The Message Passing Mechanism |
|
|
62 | (1) |
|
8.3.3 The Statement Terminator |
|
|
62 | (1) |
| 9 Scala Classes |
|
63 | (16) |
|
|
|
63 | (1) |
|
|
|
63 | (13) |
|
|
|
63 | (1) |
|
9.2.2 Developing a Class Definition |
|
|
64 | (2) |
|
9.2.3 Classes and Messages |
|
|
66 | (1) |
|
9.2.4 Instances and Instance Variables |
|
|
66 | (1) |
|
9.2.5 Classes and Inheritance |
|
|
67 | (2) |
|
|
|
69 | (2) |
|
|
|
71 | (2) |
|
9.2.8 Auxiliary Constructors |
|
|
73 | (1) |
|
9.2.9 Class Initialisation Behaviour |
|
|
74 | (1) |
|
9.2.10 Review of classes and constructors |
|
|
75 | (1) |
|
|
|
76 | (3) |
|
|
|
77 | (2) |
| 10 Scala Methods |
|
79 | (14) |
|
|
|
79 | (1) |
|
|
|
79 | (11) |
|
|
|
81 | (1) |
|
|
|
82 | (1) |
|
10.2.3 The Local Variables Section |
|
|
83 | (1) |
|
10.2.4 The Statements Section |
|
|
83 | (2) |
|
10.2.5 The Return Operator |
|
|
85 | (1) |
|
|
|
85 | (1) |
|
10.2.7 Overriding toString |
|
|
86 | (2) |
|
10.2.8 Defining Property Methods |
|
|
88 | (2) |
|
|
|
90 | (3) |
| 11 Building a Class |
|
93 | (12) |
|
|
|
93 | (1) |
|
11.2 Create a New Project |
|
|
93 | (1) |
|
11.3 Create a New Package |
|
|
93 | (3) |
|
|
|
96 | (1) |
|
|
|
96 | (2) |
|
|
|
98 | (1) |
|
|
|
99 | (2) |
|
|
|
101 | (2) |
|
|
|
103 | (2) |
| 12 Packages & Encapsulation |
|
105 | (18) |
|
|
|
105 | (1) |
|
|
|
105 | (9) |
|
12.2.1 Declaring a Package |
|
|
106 | (1) |
|
12.2.2 Additional Package Definitions Options |
|
|
106 | (5) |
|
12.2.3 An Example Package |
|
|
111 | (1) |
|
12.2.4 Accessing Package Elements |
|
|
112 | (1) |
|
12.2.5 An Example of Using a Package |
|
|
113 | (1) |
|
|
|
114 | (1) |
|
12.4 Additional Import Features |
|
|
115 | (1) |
|
|
|
116 | (1) |
|
|
|
117 | (1) |
|
|
|
118 | (1) |
|
|
|
118 | (5) |
|
12.8.1 Scala Visibility Modifiers |
|
|
118 | (1) |
|
|
|
119 | (1) |
|
12.8.3 Protected Modifier |
|
|
120 | (3) |
| 13 Classes, Inheritance and Abstraction |
|
123 | (16) |
|
|
|
123 | (1) |
|
13.1.1 What Are Classes For? |
|
|
123 | (1) |
|
13.2 Inheritance Between Types |
|
|
124 | (1) |
|
13.3 Inheritance Between Classes |
|
|
125 | (5) |
|
13.3.1 The Role of a Subclass |
|
|
126 | (1) |
|
13.3.2 Capabilities of Classes |
|
|
127 | (1) |
|
13.3.3 Overriding Behaviour |
|
|
128 | (1) |
|
|
|
129 | (1) |
|
13.4 Restricting a Subclass |
|
|
130 | (2) |
|
|
|
132 | (2) |
|
|
|
134 | (1) |
|
13.7 Scala Type Hierarchy |
|
|
135 | (1) |
|
|
|
136 | (3) |
| 14 Objects and Instances |
|
139 | (8) |
|
|
|
139 | (1) |
|
|
|
139 | (2) |
|
|
|
141 | (4) |
|
14.3.1 Companion Object Behaviour |
|
|
143 | (1) |
|
14.3.2 A Object or an Instance |
|
|
144 | (1) |
|
|
|
145 | (2) |
| 15 Value Classes |
|
147 | (4) |
|
|
|
147 | (1) |
|
|
|
147 | (1) |
|
15.3 Simple Value Type Example |
|
|
148 | (1) |
|
15.4 Additional Value Class Concepts |
|
|
149 | (1) |
|
15.5 Negating Value Classes |
|
|
150 | (1) |
| 16 Scala Constructs |
|
151 | (12) |
|
|
|
151 | (1) |
|
16.2 Numbers and Numeric Operators |
|
|
151 | (2) |
|
|
|
151 | (1) |
|
16.2.2 Arithmetic Operators |
|
|
152 | (1) |
|
16.3 Characters and Strings |
|
|
153 | (1) |
|
|
|
153 | (1) |
|
|
|
153 | (1) |
|
|
|
154 | (1) |
|
|
|
155 | (4) |
|
16.5.1 Temporary Variables |
|
|
155 | (1) |
|
|
|
156 | (1) |
|
|
|
157 | (1) |
|
16.5.4 Option, Some and None |
|
|
157 | (2) |
|
|
|
159 | (1) |
|
|
|
159 | (1) |
|
16.6 Messages and Message Selectors |
|
|
159 | (2) |
|
|
|
159 | (1) |
|
|
|
160 | (1) |
|
|
|
161 | (2) |
| 17 Control and Iteration |
|
163 | (16) |
|
|
|
163 | (1) |
|
|
|
163 | (2) |
|
|
|
163 | (2) |
|
17.2.2 If Returns a Value |
|
|
165 | (1) |
|
|
|
165 | (6) |
|
|
|
165 | (2) |
|
|
|
167 | (1) |
|
17.3.3 For Loop with a Filter |
|
|
167 | (1) |
|
17.3.4 Long Hand for Loop |
|
|
168 | (1) |
|
|
|
168 | (1) |
|
|
|
169 | (1) |
|
|
|
170 | (1) |
|
17.3.8 An Example of Loops |
|
|
171 | (1) |
|
|
|
171 | (1) |
|
|
|
172 | (7) |
|
17.5.1 The Match Expression |
|
|
174 | (5) |
| 18 Traits |
|
179 | (12) |
|
|
|
179 | (1) |
|
|
|
179 | (1) |
|
|
|
180 | (2) |
|
|
|
182 | (2) |
|
18.5 Abstract Trait Members |
|
|
184 | (1) |
|
18.6 Dynamic Binding of Traits |
|
|
184 | (2) |
|
|
|
186 | (1) |
|
|
|
186 | (1) |
|
|
|
187 | (2) |
|
18.10 To Trait or not to Trait |
|
|
189 | (2) |
| 19 Further Traits |
|
191 | (10) |
|
|
|
191 | (1) |
|
19.2 Stackable Modifications |
|
|
191 | (5) |
|
19.3 Fat versus Thin Interfaces |
|
|
196 | (1) |
|
|
|
197 | (2) |
|
19.5 Traits for a Data Type |
|
|
199 | (2) |
| 20 Arrays |
|
201 | (10) |
|
|
|
201 | (1) |
|
|
|
201 | (5) |
|
|
|
203 | (1) |
|
|
|
204 | (2) |
|
20.3 Creating Square Arrays |
|
|
206 | (2) |
|
20.4 Looping Through Arrays |
|
|
208 | (1) |
|
20.5 The Main Method Revisted |
|
|
209 | (2) |
| 21 Tuples |
|
211 | (6) |
|
|
|
211 | (1) |
|
|
|
211 | (1) |
|
21.3 Tuple Characteristics |
|
|
212 | (1) |
|
|
|
212 | (1) |
|
|
|
212 | (1) |
|
|
|
213 | (1) |
|
21.7 Iterating over a Tuple |
|
|
214 | (1) |
|
|
|
215 | (2) |
| 22 Functional Programming in Scala |
|
217 | (10) |
|
|
|
217 | (1) |
|
22.2 Scala as a Functional Language |
|
|
217 | (1) |
|
22.3 Defining Scala Functions |
|
|
218 | (3) |
|
22.4 Class, Objects and Methods |
|
|
221 | (2) |
|
|
|
223 | (4) |
| 23 Higher Order Functions |
|
227 | (6) |
|
|
|
227 | (1) |
|
23.2 Higher Order Function Concepts |
|
|
227 | (1) |
|
23.3 Scala Higher Order Functions |
|
|
228 | (2) |
|
23.4 Using Higher Order Functions |
|
|
230 | (1) |
|
23.5 Higher-Order Functions in Scala Collections |
|
|
231 | (2) |
| 24 Partially Applied Functions and Currying |
|
233 | (8) |
|
|
|
233 | (1) |
|
24.2 Partially Applied Functions |
|
|
234 | (1) |
|
|
|
235 | (6) |
|
24.3.1 Introduction to Currying |
|
|
235 | (1) |
|
24.3.2 Defining Multiple Parameter List Functions |
|
|
236 | (1) |
|
24.3.3 Using Curried Functions |
|
|
237 | (1) |
|
24.3.4 Building Domain Specific Languages |
|
|
238 | (3) |
| 25 Scala Collections Framework |
|
241 | (6) |
|
|
|
241 | (1) |
|
25.2 What are Collections? |
|
|
241 | (1) |
|
|
|
242 | (5) |
|
25.3.1 Package Scala.Collection |
|
|
243 | (1) |
|
25.3.2 Common Seq Behaviour |
|
|
244 | (1) |
|
25.3.3 Common Set Behaviour |
|
|
245 | (1) |
|
25.3.4 Common Map Behaviour |
|
|
246 | (1) |
| 26 Immutable Lists and Maps |
|
247 | (14) |
|
|
|
247 | (1) |
|
26.2 The Immutable List Collection |
|
|
247 | (10) |
|
|
|
247 | (1) |
|
26.2.2 List Concatenation |
|
|
248 | (1) |
|
|
|
249 | (2) |
|
|
|
251 | (1) |
|
26.2.5 Further List Processing |
|
|
251 | (3) |
|
|
|
254 | (1) |
|
26.2.7 Converting to a List |
|
|
255 | (1) |
|
26.2.8 Lists of User Defined Types |
|
|
255 | (2) |
|
26.3 The Immutable Map Type |
|
|
257 | (4) |
| 27 Immutable and Mutable Collection Packages |
|
261 | (10) |
|
|
|
261 | (1) |
|
27.2 Package scala.collection.immutable |
|
|
261 | (4) |
|
|
|
261 | (3) |
|
|
|
264 | (1) |
|
|
|
265 | (1) |
|
27.3 Package scala.collection.mutable |
|
|
265 | (4) |
|
|
|
266 | (1) |
|
|
|
266 | (1) |
|
|
|
267 | (1) |
|
|
|
267 | (1) |
|
|
|
268 | (1) |
|
|
|
269 | (1) |
|
|
|
270 | (1) |
| 28 Type Parameterization |
|
271 | (8) |
|
|
|
271 | (1) |
|
|
|
271 | (1) |
|
28.3 Adding Type Parameterization |
|
|
272 | (4) |
|
28.3.1 The MyQueue Mutable Class |
|
|
272 | (2) |
|
28.3.2 The Queue Immutable Class |
|
|
274 | (2) |
|
|
|
276 | (1) |
|
28.5 Lower and Upper Bounds |
|
|
277 | (1) |
|
28.6 Combining Variance and Bounds |
|
|
278 | (1) |
| 29 Further Language Constructs |
|
279 | (18) |
|
|
|
279 | (1) |
|
29.2 Implicit Conversions |
|
|
279 | (2) |
|
|
|
281 | (2) |
|
|
|
283 | (1) |
|
|
|
284 | (2) |
|
|
|
286 | (2) |
|
|
|
288 | (1) |
|
|
|
289 | (5) |
|
|
|
294 | (3) |
| 30 Exception Handling |
|
297 | (14) |
|
|
|
297 | (1) |
|
30.2 What Is an Exception? |
|
|
297 | (2) |
|
30.3 What Is Exception Handling? |
|
|
299 | (2) |
|
30.4 Throwing an Exception |
|
|
301 | (1) |
|
30.5 Catching an Exception |
|
|
301 | (4) |
|
30.6 Try Block Returns a Value |
|
|
305 | (1) |
|
30.7 Defining an Exception |
|
|
305 | (1) |
|
30.8 A More Functional Approach |
|
|
306 | (3) |
|
|
|
309 | (2) |
| 31 Scala and JDBC Database Access |
|
311 | (24) |
|
|
|
311 | (1) |
|
|
|
311 | (1) |
|
|
|
312 | (2) |
|
|
|
314 | (1) |
|
|
|
315 | (1) |
|
|
|
316 | (1) |
|
31.7 Opening a Connection |
|
|
317 | (2) |
|
31.8 Obtaining Data from a Database |
|
|
319 | (1) |
|
31.9 Inserting into a Table |
|
|
320 | (3) |
|
31.10 Update an existing Row |
|
|
323 | (1) |
|
31.11 Deleting from a Table |
|
|
323 | (1) |
|
|
|
324 | (2) |
|
|
|
326 | (1) |
|
|
|
326 | (2) |
|
|
|
328 | (3) |
|
|
|
331 | (4) |
|
|
|
331 | (1) |
|
31.16.2 ResultSetMetaData |
|
|
332 | (3) |
| 32 Scala Style Database Access |
|
335 | (6) |
|
|
|
335 | (1) |
|
|
|
335 | (1) |
|
|
|
336 | (1) |
|
|
|
337 | (1) |
|
|
|
338 | (1) |
|
|
|
339 | (2) |
| 33 Scala and MySQL Database Example |
|
341 | (18) |
|
|
|
341 | (1) |
|
|
|
341 | (1) |
|
33.2.1 Starting/Stopping/Connecting to MySQL |
|
|
341 | (1) |
|
|
|
342 | (3) |
|
|
|
343 | (1) |
|
33.3.2 Selecting to Work with a Database |
|
|
343 | (1) |
|
|
|
344 | (1) |
|
33.3.4 Adding Data to a Table |
|
|
345 | (1) |
|
33.4 Creating the Scala Project |
|
|
345 | (1) |
|
33.5 Create a New Package |
|
|
345 | (1) |
|
33.6 Obtaining the JDBC Libraries |
|
|
346 | (1) |
|
33.7 Modifying the Project Properties |
|
|
347 | (2) |
|
33.8 Accessing the Database |
|
|
349 | (8) |
|
33.9 Running the Application |
|
|
357 | (2) |
| 34 Testing |
|
359 | (6) |
|
|
|
359 | (1) |
|
|
|
359 | (1) |
|
34.3 What should be Tested? |
|
|
360 | (1) |
|
|
|
360 | (4) |
|
|
|
362 | (1) |
|
34.4.2 Integration Testing |
|
|
362 | (1) |
|
|
|
363 | (1) |
|
34.4.4 Installation Testing |
|
|
363 | (1) |
|
|
|
364 | (1) |
|
|
|
364 | (1) |
| 35 Scala Testing |
|
365 | (18) |
|
|
|
365 | (1) |
|
35.2 Scala Runtime Test Facilities |
|
|
365 | (2) |
|
|
|
365 | (1) |
|
35.2.2 Using Require and Assert |
|
|
366 | (1) |
|
35.3 Test Libraries in Scala |
|
|
367 | (1) |
|
|
|
367 | (1) |
|
|
|
367 | (1) |
|
|
|
367 | (1) |
|
35.4 ScalaTest Testing Framework |
|
|
368 | (10) |
|
35.4.1 Setting up your Scala Project |
|
|
368 | (4) |
|
35.4.2 ScalaTest and JUnit |
|
|
372 | (5) |
|
35.4.3 Scala Test and Functional Test Suites |
|
|
377 | (1) |
|
35.5 ScalaTest and Feature Tests |
|
|
378 | (2) |
|
35.6 Test Driven Development |
|
|
380 | (2) |
|
|
|
380 | (1) |
|
|
|
381 | (1) |
|
|
|
382 | (1) |
|
|
|
382 | (1) |
|
|
|
382 | (1) |
| 36 Introduction to Akka Actors |
|
383 | (16) |
|
|
|
383 | (1) |
|
|
|
383 | (1) |
|
|
|
384 | (1) |
|
|
|
385 | (1) |
|
36.5 Akka Scala Actor library |
|
|
386 | (2) |
|
36.6 Concurrent Hello World |
|
|
388 | (2) |
|
|
|
390 | (2) |
|
|
|
392 | (1) |
|
|
|
392 | (3) |
|
|
|
395 | (1) |
|
|
|
396 | (3) |
| 37 Further Akka Actors |
|
399 | (14) |
|
|
|
399 | (1) |
|
37.2 Generating a Result from an Actor |
|
|
399 | (2) |
|
|
|
401 | (3) |
|
|
|
404 | (3) |
|
|
|
407 | (1) |
|
|
|
408 | (3) |
|
|
|
411 | (1) |
|
|
|
412 | (1) |
| 38 Play Framework |
|
413 | (16) |
|
|
|
413 | (1) |
|
38.2 Introduction to Play |
|
|
413 | (2) |
|
|
|
415 | (4) |
|
|
|
415 | (1) |
|
|
|
415 | (1) |
|
38.3.3 Setting up the Play Environment |
|
|
415 | (1) |
|
38.3.4 Creating a New Web Application |
|
|
416 | (3) |
|
38.4 Starting the Web Application |
|
|
419 | (1) |
|
38.5 Editing the Application |
|
|
420 | (3) |
|
38.5.1 Importing into Eclipse |
|
|
420 | (1) |
|
38.5.2 Working with the Application |
|
|
421 | (2) |
|
38.6 Model View Controller |
|
|
423 | (2) |
|
38.7 Exploring the Play Application |
|
|
425 | (4) |
| 39 RESTful Services |
|
429 | (14) |
|
|
|
429 | (1) |
|
|
|
429 | (1) |
|
|
|
430 | (1) |
|
39.4 Creating the RESTful Web Application |
|
|
431 | (4) |
|
39.5 JavaScript and jQuery |
|
|
435 | (1) |
|
|
|
436 | (5) |
|
|
|
441 | (2) |
| 40 GUIs in Scala Swing |
|
443 | (12) |
|
|
|
443 | (1) |
|
|
|
443 | (1) |
|
|
|
444 | (1) |
|
|
|
444 | (2) |
|
40.5 Scala Swing Packages |
|
|
446 | (1) |
|
40.6 Swing Scala Worked Examples |
|
|
447 | (8) |
|
40.6.1 Simple Hello World UI |
|
|
447 | (1) |
|
40.6.2 Panels and UI Layout |
|
|
448 | (2) |
|
40.6.3 Working with a BorderPanel |
|
|
450 | (1) |
|
40.6.4 Working with a BoxPanel |
|
|
451 | (2) |
|
40.6.5 Displaying a Table |
|
|
453 | (2) |
| 41 User Input in Scala Swing |
|
455 | (10) |
|
|
|
455 | (1) |
|
|
|
455 | (7) |
|
41.2.1 Scala Swing Actions |
|
|
458 | (2) |
|
41.2.2 Working with Menus |
|
|
460 | (2) |
|
41.3 A Simple GUI Example |
|
|
462 | (3) |
| 42 Scala Build Tools |
|
465 | (14) |
|
|
|
465 | (1) |
|
42.2 Why we need a Build Tool |
|
|
465 | (1) |
|
|
|
466 | (2) |
|
42.3.1 Maven Repositories |
|
|
468 | (1) |
|
|
|
468 | (1) |
|
|
|
469 | (3) |
|
42.6 Maven Lifecycle Commands |
|
|
472 | (1) |
|
|
|
472 | (5) |
|
42.7.1 Creating an SBT Project |
|
|
474 | (2) |
|
42.7.2 SBT Lifecycle Commands |
|
|
476 | (1) |
|
|
|
477 | (2) |
| 43 Scala & Java Interoperability |
|
479 | |
|
|
|
479 | (1) |
|
|
|
479 | (1) |
|
|
|
480 | (1) |
|
|
|
481 | (7) |
|
|
|
482 | (1) |
|
|
|
483 | (2) |
|
|
|
485 | (3) |
|
|
|
488 | (2) |
|
|
|
490 | (2) |
|
43.7 Implementing a Java Interface |
|
|
492 | |