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/overview/a-taste-of-scala.md
+13-20Lines changed: 13 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -239,19 +239,21 @@ val mi = 'C'
239
239
vallastName="Doe"
240
240
```
241
241
242
-
String interpolation lets you combine those variables in a string like this:
242
+
You can combine those variables in a string like this:
243
243
244
244
```scala
245
245
println(s"Name: $firstName$mi$lastName") // "Name: John C Doe"
246
246
```
247
247
248
248
Just precede the string with the letter `s`, and then put a `$` symbol before your variable names inside the string.
249
249
250
-
For expressions more complex than a single identifier, enclose the expression in curly braces:
250
+
To enclose expressions inside a string, enclose them in curly braces:
251
251
252
-
```scala
252
+
~~~scala
253
253
println(s"2 + 2 = ${2+2}") // prints "2 + 2 = 4"
254
-
```
254
+
valx=-1
255
+
println(s"x.abs = ${x.abs}") // prints "x.abs = 1"
256
+
~~~
255
257
256
258
#### Other interpolators
257
259
@@ -347,7 +349,7 @@ else
347
349
Note that this really is an *expression* — not a *statement* — meaning that it returns a value, so you can assign the result to a variable:
348
350
349
351
```scala
350
-
valx=if(a < b) a else b
352
+
valx=if a < bthen a else b
351
353
```
352
354
353
355
As you’ll see throughout this Overview and in our Reference documentation, *all* Scala control structures can be used as expressions.
@@ -369,7 +371,7 @@ The `for` keyword can be used to create a `for` loop. This example shows how to
369
371
```scala
370
372
valints=List(1,2,3,4,5)
371
373
372
-
for(i <- ints) println(i)
374
+
for i <- intsdo println(i)
373
375
```
374
376
375
377
<!--
@@ -410,15 +412,14 @@ We encourage you to make changes to that code to be sure you understand how it w
410
412
411
413
#### Using `for` as an expression
412
414
413
-
The `for` keyword has even more power: When you add the `yield` keyword to `for` loops, you create powerful`for`*expressions* which are used to calculate and yield results.
415
+
The `for` keyword has even more power: When you use the `yield` keyword instead of `do`, you create `for`*expressions* which are used to calculate and yield results.
414
416
415
417
A few examples demonstrate this. Using the same `ints` list as the previous example, this code creates a new list, where the value of each element in the new list is twice the value of the elements in the original list:
416
418
417
419
````
418
420
scala> val doubles = for (i <- ints) yield i * 2
419
421
val doubles: List[Int] = List(2, 4, 6, 8, 10)
420
422
````
421
-
422
423
Note that Scala’s syntax is flexible, and that `for` expression can be written in several different ways to make your code more readable:
423
424
424
425
```scala
@@ -591,7 +592,7 @@ trait Runner:
591
592
Given those traits, here’s a `Dog` class that extends all of those traits while providing a behavior for the abstract `speak` method:
0 commit comments