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
- do a search/replace on the word “field,” make sure it’s only used in classes,
14
+
objects, traits
15
+
- don’t use "field," do use "variables," "binders," or maybe something
16
+
like "assignment"
13
17
-->
14
18
15
19
This “Taste of Scala” section provides a whirlwind tour of Scala’s main features. After the initial tour in this section, the rest of the Overview will provide a few more details on the these features, and the Reference documentation will provide *many* more details.
@@ -22,7 +26,7 @@ This “Taste of Scala” section provides a whirlwind tour of Scala’s main fe
22
26
23
27
<!-- NOTE: i assume here that dotc/dotr will be renamed to scala/scala -->
24
28
25
-
A “Hello, world” example in Scala goes as follows. First, Put this code in a file named *Hello.scala*:
29
+
A “Hello, world” example in Scala goes as follows. First, put this code in a file named *Hello.scala*:
26
30
27
31
```scala
28
32
@main defhello= println("Hello, world")
@@ -81,7 +85,7 @@ scala> 2 + 2
81
85
val res1: Int = 4
82
86
````
83
87
84
-
As shown in the output, if you don’t assign a variable to the result of an expression, the REPL creates variables named `res0`, `res1`, etc., for you. You can use these variable names in future expressions:
88
+
As shown in the output, if you don’t assign a variable to the result of an expression, the REPL creates variables named `res0`, `res1`, etc., for you. You can use these variable names in subsequent expressions:
85
89
86
90
````
87
91
scala> val x = res0 * 10
@@ -121,22 +125,20 @@ var c = 1 // mutable
121
125
vard="world"
122
126
```
123
127
124
-
In an application, a `val`field can’t be reassigned. You’ll generate a compiler error if you try to reassign one:
128
+
In an application, a `val` can’t be reassigned. You’ll generate a compiler error if you try to reassign one:
125
129
126
130
```scala
127
131
valmsg="Hello, world"
128
132
msg ="Hello"// "reassignment to val" error; this won’t compile
129
133
```
130
134
131
-
Conversely, a `var`field can be reassigned:
135
+
Conversely, a `var` can be reassigned:
132
136
133
137
```scala
134
138
varmsg="Hello, world"
135
139
msg ="Hello"// this compiles because a var can be reassigned
136
140
```
137
141
138
-
>Inside the REPL playground area you can reassign a `val` field, but this is just for convenience.
139
-
140
142
141
143
142
144
## Declaring variable types
@@ -245,16 +247,9 @@ println(s"Name: $firstName $mi $lastName") // "Name: John C Doe"
245
247
246
248
Just precede the string with the letter `s`, and then put a `$` symbol before your variable names inside the string.
247
249
248
-
To enclose class fields, class methods, or equations inside a string, enclose them in curly braces:
250
+
For expressions more complex than a single identifier, enclose the expression in curly braces:
249
251
250
252
```scala
251
-
classPerson(varname:String)
252
-
valp=Person("Margaret Hamilton")
253
-
254
-
// a class field or method
255
-
println(s"Name: ${p.name}") // "Name: Margaret Hamilton"
256
-
257
-
// an equation
258
253
println(s"2 + 2 = ${2+2}") // prints "2 + 2 = 4"
259
254
```
260
255
@@ -417,19 +412,21 @@ We encourage you to make changes to that code to be sure you understand how it w
417
412
418
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.
419
414
420
-
A few examples demonstrate this. Given this list:
421
-
422
-
```scala
423
-
valnums=List(1,2,3,4,5)
424
-
```
425
-
426
-
This code creates a new list where each element in the new list is twice the amount of the elements in the original list:
415
+
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:
427
416
428
417
````
429
-
scala> val doubles = for i <- nums yield i * 2
418
+
scala> val doubles = for (i <- ints) yield i * 2
430
419
val doubles: List[Int] = List(2, 4, 6, 8, 10)
431
420
````
432
421
422
+
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
+
```scala
425
+
valdoubles=for i <- ints yield i *2
426
+
valdoubles=for (i <- ints) yield i *2
427
+
valdoubles=for (i <- ints) yield (i *2)
428
+
```
429
+
433
430
This example shows how to capitalize the first character in each string in the list:
434
431
435
432
```scala
@@ -506,7 +503,7 @@ getClassAsString("hello") // 'hello' is a String
506
503
getClassAsString(List(1,2,3)) // List
507
504
```
508
505
509
-
Pattern matches can get as complicated as you need, and you’ll see more examples of it in the Control Structures sections of this Overview and in the Reference documentation.
506
+
There’s much more to pattern matching in Scala. Patterns can be nested, results of patterns can be bound, and pattern matching can even be user-defined. You can find more pattern matching examples in the Control Structures section of this Overview, and in the Reference documentation.
510
507
511
508
512
509
### try/catch/finally
@@ -553,10 +550,7 @@ do
553
550
554
551
### Create your own control structures!
555
552
556
-
In Scala you can also create code that works just like a control structure. You can learn more about this in the Control Structures chapter in the Reference documentation.
557
-
<!--
558
-
NOTE: my old examples are `whilst` and `doubleIf`; `using` is a good example.
559
-
-->
553
+
Thanks to features like by-name parameters, infix notation, fluent interfaces, optional parentheses, extension methods, and higher-order functions, you can also create code that works just like a control structure. You’ll learn more about this in the Control Structures chapter in the Reference documentation.
560
554
561
555
562
556
@@ -645,7 +639,7 @@ If that code makes sense — great, you’re comfortable with traits as interfac
645
639
- Traits or abstract classes are always open, so open is redundant for them.
646
640
-->
647
641
648
-
Scala *classes* are used in OOP-style programming. Here’s an example of a class that models a “person.” The first and last names can both be modified, so they’re declared as `var` parameters:
642
+
Scala *classes* are used in OOP-style programming. Here’s an example of a class that models a “person.” In OOP, fields are typically mutable, so `firstName` and `lastName` are both declared as `var` parameters:
@@ -731,6 +725,11 @@ Enums are covered in detail in the Data Modeling section of this Overview, and i
731
725
732
726
#### Case classes
733
727
728
+
<!--
729
+
- WIKI: includes automatic support for pattern matching
730
+
- (From the perspective of Scala, a case class is simply a normal class for which the compiler automatically adds certain behaviors that could also be provided manually, e.g., definitions of methods providing for deep comparisons and hashing, and destructuring a case class on its constructor parameters during pattern matching.)
731
+
-->
732
+
734
733
A *case class* is an extension of the base Scala class. Case classes provide features that make them useful for functional programming. They have all of the functionality of a regular class, and more. When the compiler sees the `case` keyword in front of a `class`, it generates code for you, with these benefits:
735
734
736
735
- Case class constructor parameters are public `val` fields by default, so accessor methods are generated for each parameter.
@@ -939,32 +938,27 @@ In addition to HOFs used all throughout the standard library, you can easily cre
939
938
## Extension methods
940
939
941
940
<!-- TODO: i may not be showing the latest syntax -->
942
-
*Extension methods* let you add new methods to closed classes. For instance, if you want to add a `hello`method to the `String` class, just create an extension method:
941
+
*Extension methods* let you add new methods to closed classes. For instance, if you want to add two methods named `hello`and `aloha`to the `String` class, just declare them as extension methods:
The `extension` keyword declares that you’re about to define one or more extension methods. As shown, the `s` parameter can then be used in your methods.
962
954
963
955
This next example shows how to add a `makeInt` method to the `String` class. Here, `makeInt` takes a parameter named `radix`. The code doesn’t account for possible string-to-integer conversion errors, but skipping that detail, the examples show how it works:
|`List`||√| a linear (linked list), immutable sequence |
983
-
|`Map`|√|√| the base `Map` (key/value pairs) class |
984
-
|`Set`|√|√| the base `Set` class |
985
-
|`Vector`||√| an indexed, immutable sequence |
975
+
|`ArrayBuffer`|✓|| an indexed, mutable sequence |
976
+
|`List`||✓| a linear (linked list), immutable sequence |
977
+
|`Map`|✓|✓| the base `Map` (key/value pairs) class |
978
+
|`Set`|✓|✓| the base `Set` class |
979
+
|`Vector`||✓| an indexed, immutable sequence |
986
980
987
981
Scala also has a `Range` class which lets you create ordered sequences of integers that are equally spaced apart, such as “1, 2, 3,” and “5, 8, 11, 14.” Ranges are often used in `for` loops, and to create other sequences:
0 commit comments