Skip to content

Commit 668cf19

Browse files
committed
many small tweaks to the Basics page
all of these changes are small, don't involve changing ordering, and are hopefully not controversial.
1 parent dca716f commit 668cf19

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

tutorials/tour/basics.md

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ In this page, we will cover basics of Scala.
1414

1515
## Trying Scala in the Browser
1616

17-
You can run Scala in your browser by using [ScalaFiddle](https://scalafiddle.io).
17+
You can run Scala in your browser with [ScalaFiddle](https://scalafiddle.io).
1818

1919
1. Go to https://scalafiddle.io.
20-
2. Copy and paste `println("Hello, world!")` to the left pane.
21-
3. Hit "Run" button and see it prints "Hello, world!" on the right pane.
20+
2. Paste `println("Hello, world!")` in the left pane.
21+
3. Hit "Run" button. Output appears in the right pane.
2222

23-
It is a perfect way for anybody to experiment with a piece of Scala code anywhere, anytime!
23+
This is an easy, zero-setup way to experiment with pieces of Scala code.
2424

2525
## Expressions
2626

@@ -37,14 +37,15 @@ println("Hello," + " world!") // Hello, world!
3737

3838
### Values
3939

40-
You can name results of expressions by using the `val` keyword.
40+
You can name results of expressions with the `val` keyword.
4141

4242
```
4343
val x = 1 + 1
44-
println(x) // 2
44+
println(x * x) // 4
4545
```
4646

47-
Named results are called values. For example, in above case, `x` is called a value. Values only hold results of expressions, so they will not re-compute expressions when referenced.
47+
Named results, such as `x` here, are called values. Referencing
48+
a value does not re-compute it.
4849

4950
Values cannot be re-assigned.
5051

@@ -53,33 +54,33 @@ val x = 1 + 1
5354
x = 3 // This does not compile.
5455
```
5556

56-
Types of values can be inferred, but you can also explicitly state types like below.
57+
Types of values can be inferred, but you can also explicitly state the type, like this:
5758

5859
```
5960
val x: Int = 1 + 1
6061
```
6162

6263
### Variables
6364

64-
Variables are similar to values except you can re-assign results they hold. You can define variables with the `var` keyword.
65+
Variables are like values, except you can re-assign them. You can define a variable with the `var` keyword.
6566

6667
```
6768
var x = 1 + 1
6869
x = 3 // This compiles because "x" is declared with the "var" keyword.
69-
println(x) // 3
70+
println(x * x) // 9
7071
```
7172

72-
Just like values, you can also explicitly state types of variables like below.
73+
As with values, you can explicitly state the type if you want:
7374

7475
```
7576
var x: Int = 1 + 1
7677
```
7778

7879
### Blocks
7980

80-
You can create a single expression out of multiple statements by wrapping them with `{}`. We call them blocks or block expressions.
81+
You can combine expressions by surrounding them with `{}`. We call this a block.
8182

82-
The result of the last statement of the block will be the result of the overall block.
83+
The result of the last expression in the block is the result of the overall block, too.
8384

8485
```
8586
println({
@@ -90,15 +91,15 @@ println({
9091

9192
## Functions
9293

93-
Functions are expressions with parameters.
94+
Functions are expressions that take parameters and compute a result.
9495

95-
You can define a function that returns a given integer + 1 like below.
96+
You can define a function that returns a given integer plus one:
9697

9798
```
9899
(x: Int) => x + 1
99100
```
100101

101-
Left hand side of `=>` is a list of parameters and right hand side of `=>` is an expression taking the parameters.
102+
On the left of `=>` is a list of parameters. On the right is an expression involving the parameters.
102103

103104
You can also name functions.
104105

@@ -107,7 +108,7 @@ val addOne = (x: Int) => x + 1
107108
println(addOne(1)) // 2
108109
```
109110

110-
Or it can take multiple parameters.
111+
Functions may take multiple parameters.
111112

112113
```
113114
val add = (x: Int, y: Int) => x + y
@@ -127,7 +128,7 @@ We will cover functions in depth [later](anonymous-function-syntax.md).
127128

128129
Methods look and behave very similar to functions, but there are a few key differences between them.
129130

130-
Methods are defined like below with the `def` keyword followed by its name, a list of parameter groups, return type, and an expression.
131+
Methods are defined with the `def` keyword. `def` is followed by a name, parameter lists, a return type, and a body.
131132

132133
```
133134
def add(x: Int, y: Int): Int = x + y
@@ -142,14 +143,14 @@ val add2 = add // This does not compile.
142143
var add3 = add // This does not compile either.
143144
```
144145

145-
Methods can take multiple parameter groups.
146+
Methods can take multiple parameter lists.
146147

147148
```
148149
def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y) * multiplier
149150
println(addThenMultiply(1, 2)(3)) // 9
150151
```
151152

152-
Or no parameter groups at all.
153+
Or no parameter lists at all.
153154

154155
```
155156
def name: String = System.getProperty("name")
@@ -163,12 +164,13 @@ There are some other differences, but for now, you can think of them as somethin
163164
You can define classes with the `class` keyword followed by its name and constructor parameters.
164165

165166
```
166-
class Greeter(prefix: String, postfix: String) {
167-
def greet(name: String): Unit = println(prefix + name + postfix)
167+
class Greeter(prefix: String, suffix: String) {
168+
def greet(name: String): Unit =
169+
println(prefix + name + suffix)
168170
}
169171
```
170172

171-
You can instantiate classes with the `new` keyword.
173+
You can make an instance of a class with the `new` keyword.
172174

173175
```
174176
val greeter = new Greeter("Hello, ", "!")
@@ -179,7 +181,7 @@ We will cover classes in depth [later](classes.md).
179181

180182
## Case Classes
181183

182-
Scala has a special type of class called case class that's immutable and compared by value by default. You can define case classes with the `case class` keyword.
184+
Scala has a special type of class called a "case" class. By default, case classes are immutable and compared by value. You can define case classes with the `case class` keywords.
183185

184186
```
185187
case class Point(x: Int, y: Int)
@@ -193,7 +195,7 @@ val anotherPoint = Point(1, 2)
193195
val yetAnotherPoint = Point(2, 2)
194196
```
195197

196-
And they are compared by values.
198+
And they are compared by value.
197199

198200
```
199201
if (point == anotherPoint) {
@@ -222,15 +224,14 @@ You can define objects with the `object` keyword.
222224
```
223225
object IdFactory {
224226
private var counter = 0
225-
226227
def create(): Int = {
227228
counter += 1
228229
counter
229230
}
230231
}
231232
```
232233

233-
You can access objects by referring its name.
234+
You can access an object by referring to its name.
234235

235236
```
236237
val newId: Int = IdFactory.create()
@@ -243,7 +244,7 @@ We will cover objects in depth [later](singleton-objects.md).
243244

244245
## Traits
245246

246-
Traits define specification of types as signature of fields and methods.
247+
Traits are types containing certain fields and methods. Multiple traits can be combined.
247248

248249
You can define traits with `trait` keyword.
249250

@@ -257,11 +258,12 @@ Traits can also have default implementations.
257258

258259
```
259260
trait Greeter {
260-
def greet(name: String): Unit = println("Hello, " + name + "!")
261+
def greet(name: String): Unit =
262+
println("Hello, " + name + "!")
261263
}
262264
```
263265

264-
You can extend traits with the `extends` keyword and override their implementations with the `override` keyword.
266+
You can extend traits with the `extends` keyword and override an implementation with the `override` keyword.
265267

266268
```
267269
class DefaultGreeter extends Greeter
@@ -279,16 +281,21 @@ val customGreeter = new CustomizableGreeter("How are you, ", "?")
279281
customGreeter.greet("Scala developer") // How are you, Scala developer?
280282
```
281283

284+
Here, `DefaultGreeter` extends only a single trait, but it could extend multiple traits.
285+
282286
We will cover traits in depth [later](traits.md).
283287

284288
## Main Method
285289

286-
Main method is an entry point of a program.
290+
The main method is an entry point of a program. The Java Virtual
291+
Machine requires a main method to be named `main` and take one
292+
argument, an array of strings.
287293

288-
Using objects, you can define main methods like below.
294+
Using an object, you can define a main method as follows:
289295

290296
```
291297
object Main {
292-
def main(args: Array[String]): Unit = println("Hello, Scala developer!")
298+
def main(args: Array[String]): Unit =
299+
println("Hello, Scala developer!")
293300
}
294301
```

0 commit comments

Comments
 (0)