Skip to content

Commit 179ad5a

Browse files
adpi2julienrf
authored andcommitted
Add Scala 2 tabs in scala-3-book/scala-features
Also rename the page to Scala Features
1 parent 6d603cc commit 179ad5a

File tree

1 file changed

+88
-9
lines changed

1 file changed

+88
-9
lines changed

_overviews/scala3-book/scala-features.md

+88-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: Scala 3 Features
2+
title: Scala Features
33
type: chapter
4-
description: This page discusses the main features of the Scala 3 programming language.
4+
description: This page discusses the main features of the Scala programming language.
55
num: 2
66
previous-page: introduction
77
next-page: why-scala-3
@@ -50,27 +50,50 @@ Second, with the use of lambdas and higher-order functions, you write your code
5050
As the functional programming saying goes, in Scala you write _what_ you want, not _how_ to achieve it.
5151
That is, we don’t write imperative code like this:
5252

53+
{% tabs scala-features-1 class=tabs-scala-version %}
54+
{% tab 'Scala 2' for=scala-features-1 %}
5355
```scala
5456
import scala.collection.mutable.ListBuffer
5557

5658
def double(ints: List[Int]): List[Int] = {
5759
val buffer = new ListBuffer[Int]()
5860
for (i <- ints) {
59-
buffer += i * 2
61+
buffer += i * 2
6062
}
6163
buffer.toList
6264
}
6365

6466
val oldNumbers = List(1, 2, 3)
6567
val newNumbers = double(oldNumbers)
6668
```
69+
{% endtab %}
70+
{% tab 'Scala 3' for=scala-features-1 %}
71+
```scala
72+
import scala.collection.mutable.ListBuffer
73+
74+
def double(ints: List[Int]): List[Int] =
75+
val buffer = new ListBuffer[Int]()
76+
for i <- ints do
77+
buffer += i * 2
78+
buffer.toList
79+
80+
val oldNumbers = List(1, 2, 3)
81+
val newNumbers = double(oldNumbers)
82+
```
83+
{% endtab %}
84+
{% endtabs %}
6785

6886
That code instructs the compiler what to do on a step-by-step basis.
6987
Instead, we write high-level, functional code using higher-order functions and lambdas like this to compute the same result:
7088

89+
{% tabs scala-features-2 %}
90+
{% tab 'Scala 2 and 3' for=scala-features-2 %}
7191
```scala
7292
val newNumbers = oldNumbers.map(_ * 2)
7393
```
94+
{% endtab %}
95+
{% endtabs %}
96+
7497

7598
As you can see, that code is much more concise, easier to read, and easier to maintain.
7699

@@ -80,23 +103,50 @@ As you can see, that code is much more concise, easier to read, and easier to ma
80103
Scala has a concise, readable syntax.
81104
For instance, variables are created concisely, and their types are clear:
82105

106+
{% tabs scala-features-3 %}
107+
{% tab 'Scala 2 and 3' for=scala-features-3 %}
83108
```scala
84109
val nums = List(1,2,3)
85110
val p = Person("Martin", "Odersky")
86111
```
112+
{% endtab %}
113+
{% endtabs %}
114+
87115

88116
Higher-order functions and lambdas make for concise code that’s readable:
89117

118+
{% tabs scala-features-4 %}
119+
{% tab 'Scala 2 and 3' for=scala-features-4 %}
90120
```scala
91121
nums.map(i => i * 2) // long form
92122
nums.map(_ * 2) // short form
93123

94124
nums.filter(i => i > 1)
95125
nums.filter(_ > 1)
96126
```
127+
{% endtab %}
128+
{% endtabs %}
97129

98130
Traits, classes, and methods are defined with a clean, light syntax:
99131

132+
{% tabs scala-features-5 class=tabs-scala-version %}
133+
{% tab 'Scala 2' for=scala-features-5 %}
134+
```scala mdoc
135+
trait Animal {
136+
def speak(): Unit
137+
}
138+
139+
trait HasTail {
140+
def wagTail(): Unit
141+
}
142+
143+
class Dog extends Animal with HasTail {
144+
def speak(): Unit = println("Woof")
145+
def wagTail(): Unit = println("⎞⎜⎛ ⎞⎜⎛")
146+
}
147+
```
148+
{% endtab %}
149+
{% tab 'Scala 3' for=scala-features-5 %}
100150
```scala
101151
trait Animal:
102152
def speak(): Unit
@@ -105,9 +155,12 @@ trait HasTail:
105155
def wagTail(): Unit
106156

107157
class Dog extends Animal, HasTail:
108-
def speak() = println("Woof")
109-
def wagTail() = println("⎞⎜⎛ ⎞⎜⎛")
158+
def speak(): Unit = println("Woof")
159+
def wagTail(): Unit = println("⎞⎜⎛ ⎞⎜⎛")
110160
```
161+
{% endtab %}
162+
{% endtabs %}
163+
111164

112165
Studies have shown that the time a developer spends _reading_ code to _writing_ code is at least a 10:1 ratio, so writing code that is concise _and_ readable is important.
113166

@@ -117,15 +170,33 @@ Studies have shown that the time a developer spends _reading_ code to _writing_
117170
Scala is a statically-typed language, but thanks to its type inference capabilities it feels dynamic.
118171
All of these expressions look like a dynamically-typed language like Python or Ruby, but they’re all Scala:
119172

173+
{% tabs scala-features-6 class=tabs-scala-version %}
174+
{% tab 'Scala 2' for=scala-features-6 %}
175+
```scala
176+
val s = "Hello"
177+
val p = Person("Al", "Pacino")
178+
val sum = nums.reduceLeft(_ + _)
179+
val y = for (i <- nums) yield i * 2
180+
val z = nums
181+
.filter(_ > 100)
182+
.filter(_ < 10_000)
183+
.map(_ * 2)
184+
```
185+
{% endtab %}
186+
{% tab 'Scala 3' for=scala-features-6 %}
120187
```scala
121188
val s = "Hello"
122189
val p = Person("Al", "Pacino")
123190
val sum = nums.reduceLeft(_ + _)
124191
val y = for i <- nums yield i * 2
125-
val z = nums.filter(_ > 100)
126-
.filter(_ < 10_000)
127-
.map(_ * 2)
192+
val z = nums
193+
.filter(_ > 100)
194+
.filter(_ < 10_000)
195+
.map(_ * 2)
128196
```
197+
{% endtab %}
198+
{% endtabs %}
199+
129200

130201
As Heather Miller states, Scala is considered to be a [strong, statically-typed language](https://heather.miller.am/blog/types-in-scala.html), and you get all the benefits of static types:
131202

@@ -266,20 +337,28 @@ In regards to the second point, large libraries like [Akka](https://akka.io) and
266337
In regards to the first point, Java classes and libraries are used in Scala applications every day.
267338
For instance, in Scala you can read files with a Java `BufferedReader` and `FileReader`:
268339

340+
{% tabs scala-features-7 %}
341+
{% tab 'Scala 2 and 3' for=scala-features-7 %}
269342
```scala
270343
import java.io.*
271344
val br = BufferedReader(FileReader(filename))
272345
// read the file with `br` ...
273346
```
347+
{% endtab %}
348+
{% endtabs %}
274349

275350
Using Java code in Scala is generally seamless.
276351

277352
Java collections can also be used in Scala, and if you want to use Scala’s rich collection class methods with them, you can convert them with just a few lines of code:
278353

354+
{% tabs scala-features-8 %}
355+
{% tab 'Scala 2 and 3' for=scala-features-8 %}
279356
```scala
280357
import scala.jdk.CollectionConverters.*
281358
val scalaList: Seq[Integer] = JavaClass.getJavaList().asScala.toSeq
282359
```
360+
{% endtab %}
361+
{% endtabs %}
283362

284363

285364
### Wealth of libraries
@@ -303,7 +382,7 @@ Assuming you told someone about the previous high-level features and then they s
303382

304383
## Lower-level language features
305384

306-
Where the previous section covered high-level features of Scala 3, it’s interesting to note that at a high level you can make the same statements about both Scala 2 and Scala 3.
385+
Where the previous section covered high-level features of Scala, it’s interesting to note that at a high level you can make the same statements about both Scala 2 and Scala 3.
307386
A decade ago Scala started with a strong foundation of desirable features, and as you’ll see in this section, those benefits have been improved with Scala 3.
308387

309388
At a “sea level” view of the details---i.e., the language features programmers use everyday---Scala 3 has significant advantages over Scala 2:

0 commit comments

Comments
 (0)