You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _overviews/scala3-book/why-scala-3.md
+88-24
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,6 @@ TODO: Is “Scala 3 Benefits” a better title?
12
12
NOTE: Could mention “grammar” as a way of showing that Scala isn’t a large language; see this slide: https://www.slideshare.net/Odersky/preparing-for-scala-3#13
13
13
{% endcomment %}
14
14
15
-
16
15
There are many benefits to using Scala, and Scala 3 in particular.
17
16
It’s hard to list every benefit of Scala, but a “Top Ten” list might look like this:
18
17
@@ -27,8 +26,6 @@ It’s hard to list every benefit of Scala, but a “Top Ten” list might look
27
26
9. The Scala ecosystem offers the most modern FP libraries in the world
28
27
10. Strong type system
29
28
30
-
31
-
32
29
## 1) FP/OOP fusion
33
30
34
31
More than any other language, Scala supports a fusion of the FP and OOP paradigms.
@@ -40,15 +37,21 @@ As Martin Odersky has stated, the essence of Scala is a fusion of functional and
40
37
Possibly some of the best examples of modularity are the classes in the standard library.
41
38
For instance, a `List` is defined as a class---technically it’s an abstract class---and a new instance is created like this:
42
39
40
+
{% tabs list %}
41
+
{% tab 'Scala 2 and 3' for=list %}
43
42
```scala
44
43
valx=List(1, 2, 3)
45
44
```
45
+
{% endtab %}
46
+
{% endtabs %}
46
47
47
48
However, what appears to the programmer to be a simple `List` is actually built from a combination of several specialized types, including traits named `Iterable`, `Seq`, and `LinearSeq`.
48
49
Those types are similarly composed of other small, modular units of code.
49
50
50
51
In addition to building a type like `List` from a series of modular traits, the `List` API also consists of dozens of other methods, many of which are higher-order functions:
valb:Password|Username=if (true) name else password
105
122
```
106
-
107
-
123
+
{% endtab %}
124
+
{% endtabs %}
108
125
109
126
## 3) Concise syntax
110
127
111
128
Scala is a low ceremony, “concise but still readable” language. For instance, variable declaration is concise:
112
129
130
+
{% tabs concise %}
131
+
{% tab 'Scala 2 and 3' for=concise %}
113
132
```scala
114
133
vala=1
115
134
valb="Hello, world"
116
135
valc=List(1,2,3)
117
136
```
137
+
{% endtab %}
138
+
{% endtabs %}
118
139
119
140
Creating types like traits, classes, and enumerations are concise:
120
141
142
+
{% tabs enum %}
143
+
{% tab 'Scala 3 Only' for=enum %}
121
144
```scala
122
145
traitTail:
123
146
defwagTail():Unit
@@ -134,18 +157,23 @@ case class Person(
134
157
age: Int
135
158
)
136
159
```
160
+
{% endtab %}
161
+
{% endtabs %}
137
162
138
163
Higher-order functions are concise:
139
164
165
+
{% tabs list-hof %}
166
+
{% tab 'Scala 2 and 3' for=list-hof %}
167
+
140
168
```scala
141
169
list.filter(_ <4)
142
170
list.map(_ *2)
143
171
```
172
+
{% endtab %}
173
+
{% endtabs %}
144
174
145
175
All of these expressions and many more are concise, and still very readable: what we call _expressive_.
146
176
147
-
148
-
149
177
## 4) Implicits, simplified
150
178
151
179
Implicits in Scala 2 were a major distinguishing design feature.
@@ -174,8 +202,6 @@ Benefits of these changes include:
174
202
175
203
These capabilities are described in detail in other sections, so see the [Contextual Abstraction introduction][contextual], and the section on [`given` and `using` clauses][given] for more details.
176
204
177
-
178
-
179
205
## 5) Seamless Java integration
180
206
181
207
Scala/Java interaction is seamless in many ways.
@@ -200,8 +226,6 @@ While almost every interaction is seamless, the [“Interacting with Java” cha
200
226
201
227
See that chapter for more details on these features.
202
228
203
-
204
-
205
229
## 6) Client & server
206
230
207
231
Scala can be used on the server side with terrific frameworks:
@@ -214,8 +238,6 @@ The Scala.js ecosystem [has dozens of libraries](https://www.scala-js.org/librar
214
238
215
239
In addition to those tools, the [Scala Native](https://github.com/scala-native/scala-native) project “is an optimizing ahead-of-time compiler and lightweight managed runtime designed specifically for Scala.” It lets you build “systems” style binary executable applications with plain Scala code, and also lets you use lower-level primitives.
216
240
217
-
218
-
219
241
## 7) Standard library methods
220
242
221
243
You will rarely ever need to write a custom `for` loop again, because the dozens of pre-built functional methods in the Scala standard library will both save you time, and help make code more consistent across different applications.
@@ -225,6 +247,8 @@ While these all use the `List` class, the same methods work with other collectio
225
247
226
248
Here are some examples:
227
249
250
+
{% tabs list-more %}
251
+
{% tab 'Scala 2 and 3' for=list-more %}
228
252
```scala
229
253
List.range(1, 3) // List(1, 2)
230
254
List.range(start =1, end =6, step =2) // List(1, 3, 5)
Scala idioms encourage best practices in many ways.
272
296
For immutability, you’re encouraged to create immutable `val` declarations:
273
297
298
+
{% tabs val %}
299
+
{% tab 'Scala 2 and 3' for=val %}
274
300
```scala
275
301
vala=1// immutable variable
276
302
```
303
+
{% endtab %}
304
+
{% endtabs %}
277
305
278
306
You’re also encouraged to use immutable collections classes like `List` and `Map`:
279
307
308
+
{% tabs list-map %}
309
+
{% tab 'Scala 2 and 3' for=list-map %}
280
310
```scala
281
311
valb=List(1,2,3) // List is immutable
282
312
valc=Map(1->"one") // Map is immutable
283
313
```
314
+
{% endtab %}
315
+
{% endtabs %}
284
316
285
317
Case classes are primarily intended for use in [domain modeling]({% link _overviews/scala3-book/domain-modeling-intro.md %}), and their parameters are immutable:
286
318
319
+
{% tabs case-class %}
320
+
{% tab 'Scala 2 and 3' for=case-class %}
287
321
```scala
288
322
caseclassPerson(name: String)
289
323
valp=Person("Michael Scott")
290
324
p.name // Michael Scott
291
325
p.name ="Joe"// compiler error (reassignment to val name)
292
326
```
327
+
{% endtab %}
328
+
{% endtabs %}
293
329
294
330
As shown in the previous section, Scala collections classes support higher-order functions, and you can pass methods (not shown) and anonymous functions into them:
`match` expressions let you use pattern matching, and they truly are _expressions_ that return values:
306
346
347
+
{% tabs match class=tabs-scala-version %}
348
+
{% tab 'Scala 2' for=match %}
349
+
```scala
350
+
valnumAsString= i match {
351
+
case1|3|5|7|9=>"odd"
352
+
case2|4|6|8|10=>"even"
353
+
case _ =>"too big"
354
+
}
355
+
```
356
+
{% endtab %}
357
+
358
+
{% tab 'Scala 3' for=match %}
307
359
```scala
308
360
valnumAsString= i match
309
361
case1|3|5|7|9=>"odd"
310
362
case2|4|6|8|10=>"even"
311
363
case _ =>"too big"
312
364
```
365
+
{% endtab %}
366
+
{% endtabs %}
313
367
314
368
Because they can return values, they’re often used as the body of a method:
315
369
370
+
{% tabs match-body class=tabs-scala-version %}
371
+
{% tab 'Scala 2' for=match-body %}
316
372
```scala
317
-
defisTruthy(a: Matchable) = a match
373
+
defisTruthy(a: Matchable) = a match {
318
374
case0|""=>false
319
375
case _ =>true
376
+
}
320
377
```
378
+
{% endtab %}
321
379
322
-
380
+
{% tab 'Scala 3' for=match-body %}
381
+
```scala
382
+
defisTruthy(a: Matchable) = a match
383
+
case0|""=>false
384
+
case _ =>true
385
+
```
386
+
{% endtab %}
387
+
{% endtabs %}
323
388
324
389
## 9) Ecosystem libraries
325
390
@@ -328,8 +393,6 @@ All of the buzzwords like high-performance, type safe, concurrent, asynchronous,
328
393
329
394
We could list hundreds of libraries here, but fortunately they’re all listed in another location: For those details, see the [“Awesome Scala” list](https://github.com/lauris/awesome-scala).
330
395
331
-
332
-
333
396
## 10) Strong type system
334
397
335
398
Scala has a strong type system, and it’s been improved even more in Scala 3.
@@ -380,7 +443,6 @@ A list of types from the Dotty documentation:
380
443
- Bounds
381
444
{% endcomment %}
382
445
383
-
384
446
_Safety_ is related to several new and changed features:
385
447
386
448
- Multiversal equality
@@ -390,6 +452,8 @@ _Safety_ is related to several new and changed features:
390
452
391
453
Good examples of _ergonomics_ are enumerations and extension methods, which have been added to Scala 3 in a very readable manner:
392
454
455
+
{% tabs extension %}
456
+
{% tab 'Scala 3 Only' for=extension %}
393
457
```scala
394
458
// enumeration
395
459
enumColor:
@@ -401,6 +465,8 @@ extension (c: Circle)
401
465
defdiameter:Double= c.radius *2
402
466
defarea:Double= math.Pi* c.radius * c.radius
403
467
```
468
+
{% endtab %}
469
+
{% endtabs %}
404
470
405
471
_Performance_ relates to several areas.
406
472
One of those is [opaque types][opaque-types].
@@ -416,8 +482,6 @@ Conversely, the goal of opaque types, as described in that SIP, is that “opera
416
482
417
483
For more type system details, see the [Reference documentation][reference].
418
484
419
-
420
-
421
485
## Other great features
422
486
423
487
Scala has many great features, and choosing a Top 10 list can be subjective.
0 commit comments