Skip to content

Commit ae7648f

Browse files
committed
Worked on the first half of Jonathan's comments.
1 parent 56ecf52 commit ae7648f

File tree

1 file changed

+42
-48
lines changed

1 file changed

+42
-48
lines changed

_overviews/overview/a-taste-of-scala.md

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ description: This page provides a high-level overview of the main features of Sc
1010
- show a method with default parameters
1111
- show package "exports"
1212
- cover Option/Try/Either (functional error handling)
13+
- 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"
1317
-->
1418

1519
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
2226

2327
<!-- NOTE: i assume here that dotc/dotr will be renamed to scala/scala -->
2428

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*:
2630

2731
```scala
2832
@main def hello = println("Hello, world")
@@ -81,7 +85,7 @@ scala> 2 + 2
8185
val res1: Int = 4
8286
````
8387

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:
8589

8690
````
8791
scala> val x = res0 * 10
@@ -121,22 +125,20 @@ var c = 1 // mutable
121125
var d = "world"
122126
```
123127

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:
125129

126130
```scala
127131
val msg = "Hello, world"
128132
msg = "Hello" // "reassignment to val" error; this won’t compile
129133
```
130134

131-
Conversely, a `var` field can be reassigned:
135+
Conversely, a `var` can be reassigned:
132136

133137
```scala
134138
var msg = "Hello, world"
135139
msg = "Hello" // this compiles because a var can be reassigned
136140
```
137141

138-
>Inside the REPL playground area you can reassign a `val` field, but this is just for convenience.
139-
140142

141143

142144
## Declaring variable types
@@ -245,16 +247,9 @@ println(s"Name: $firstName $mi $lastName") // "Name: John C Doe"
245247

246248
Just precede the string with the letter `s`, and then put a `$` symbol before your variable names inside the string.
247249

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:
249251

250252
```scala
251-
class Person(var name: String)
252-
val p = Person("Margaret Hamilton")
253-
254-
// a class field or method
255-
println(s"Name: ${p.name}") // "Name: Margaret Hamilton"
256-
257-
// an equation
258253
println(s"2 + 2 = ${2 + 2}") // prints "2 + 2 = 4"
259254
```
260255

@@ -417,19 +412,21 @@ We encourage you to make changes to that code to be sure you understand how it w
417412

418413
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.
419414

420-
A few examples demonstrate this. Given this list:
421-
422-
```scala
423-
val nums = 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:
427416

428417
````
429-
scala> val doubles = for i <- nums yield i * 2
418+
scala> val doubles = for (i <- ints) yield i * 2
430419
val doubles: List[Int] = List(2, 4, 6, 8, 10)
431420
````
432421

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+
val doubles = for i <- ints yield i * 2
426+
val doubles = for (i <- ints) yield i * 2
427+
val doubles = for (i <- ints) yield (i * 2)
428+
```
429+
433430
This example shows how to capitalize the first character in each string in the list:
434431

435432
```scala
@@ -506,7 +503,7 @@ getClassAsString("hello") // 'hello' is a String
506503
getClassAsString(List(1,2,3)) // List
507504
```
508505

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.
510507

511508

512509
### try/catch/finally
@@ -553,10 +550,7 @@ do
553550

554551
### Create your own control structures!
555552

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.
560554

561555

562556

@@ -645,7 +639,7 @@ If that code makes sense — great, you’re comfortable with traits as interfac
645639
- Traits or abstract classes are always open, so open is redundant for them.
646640
-->
647641

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:
649643

650644
```scala
651645
class Person(var firstName: String, var lastName: String):
@@ -731,6 +725,11 @@ Enums are covered in detail in the Data Modeling section of this Overview, and i
731725

732726
#### Case classes
733727

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+
734733
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:
735734

736735
- 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
939938
## Extension methods
940939

941940
<!-- 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:
943942

944943
<!-- TODO: scalafiddle -->
945944
```scala
946-
def (s: String) hello: String = s"Hello, ${s.capitalize}"
945+
extension (s: String):
946+
def hello: String = s"Hello, ${s.capitalize}"
947+
def aloha: String = s"Aloha, ${s.capitalize}"
947948

948-
"world".hello // "Hello, world"
949-
"friend".hello // "Hello, friend"
949+
"world".hello // "Hello, World"
950+
"friend".aloha // "Aloha, Friend"
950951
```
951952

952-
The comments in this code explain the extension method syntax:
953-
954-
````
955-
def (s: String) hello: String = s"Hello, ${s.capitalize}"
956-
---------- ----- ------ -------------------------
957-
^ ^ ^ ^
958-
add method method return method body
959-
to String name type
960-
class
961-
````
953+
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.
962954

963955
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:
964956

965957
<!-- TODO: scalafiddle -->
966958
```scala
967-
def (s: String) makeInt(radix: Int): Int = Integer.parseInt(s, radix)
959+
extension (s: String)
960+
def makeInt(radix: Int): Int = Integer.parseInt(s, radix)
961+
968962
"1".makeInt(2) // Int = 1
969963
"10".makeInt(2) // Int = 2
970964
"100".makeInt(2) // Int = 4
@@ -978,11 +972,11 @@ Scala has a rich set of collections classes, and those classes have a rich set o
978972

979973
| Class | Mutable | Immutable | Description |
980974
| ------------- | :-----: | :-------: | ----------- |
981-
| `ArrayBuffer` | | | an indexed, mutable sequence |
982-
| `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` | &#10003; | | an indexed, mutable sequence |
976+
| `List` | | &#10003; | a linear (linked list), immutable sequence |
977+
| `Map` | &#10003; | &#10003; | the base `Map` (key/value pairs) class |
978+
| `Set` | &#10003; | &#10003; | the base `Set` class |
979+
| `Vector` | | &#10003; | an indexed, immutable sequence |
986980

987981
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:
988982

0 commit comments

Comments
 (0)