Skip to content

Commit c6d2f45

Browse files
authored
Merge pull request #675 from SethTisue/basics-tweaks
many small tweaks to the Basics page
2 parents dca716f + 753b3ef commit c6d2f45

File tree

2 files changed

+68
-59
lines changed

2 files changed

+68
-59
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ _site
66
*~
77
vendor/bundle
88
.idea/
9+
/coursier
10+
/tut-tmp/

Diff for: tutorials/tour/basics.md

+66-59
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ 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.
1818

19-
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.
19+
1. Go to [https://scalafiddle.io](https://scalafiddle.io).
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

2727
Expressions are computable statements.
2828

2929
You can output results of expressions using `println`.
3030

31-
```
31+
```tut
3232
println(1) // 1
3333
println(1 + 1) // 2
3434
println("Hello!") // Hello!
@@ -37,51 +37,52 @@ 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

42-
```
42+
```tut
4343
val x = 1 + 1
4444
println(x) // 2
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

51-
```
52+
```tut:nofail
5253
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

58-
```
59+
```tut
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

66-
```
67+
```tut
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

74-
```
75+
```tut
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

84-
```
85+
```tut
8586
println({
8687
val x = 1 + 1
8788
x + 1
@@ -90,33 +91,33 @@ println({
9091

9192
## Functions
9293

93-
Functions are expressions with parameters.
94+
Functions are expressions that take parameters.
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

97-
```
98+
```tut
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

105-
```
106+
```tut
106107
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

112-
```
113+
```tut
113114
val add = (x: Int, y: Int) => x + y
114115
println(add(1, 2)) // 3
115116
```
116117

117118
Or it can take no parameters.
118119

119-
```
120+
```tut
120121
val getTheAnswer = () => 42
121122
println(getTheAnswer()) // 42
122123
```
@@ -127,31 +128,31 @@ 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

132-
```
133+
```tut
133134
def add(x: Int, y: Int): Int = x + y
134135
println(add(1, 2)) // 3
135136
```
136137

137138
Methods cannot be named with the `val` or `var` keywords.
138139

139-
```
140+
```tut:nofail
140141
def add(x: Int, y: Int): Int = x + y
141142
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

147-
```
148+
```tut
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

154-
```
155+
```tut
155156
def name: String = System.getProperty("name")
156157
println("Hello, " + name + "!")
157158
```
@@ -162,15 +163,16 @@ There are some other differences, but for now, you can think of them as somethin
162163

163164
You can define classes with the `class` keyword followed by its name and constructor parameters.
164165

165-
```
166-
class Greeter(prefix: String, postfix: String) {
167-
def greet(name: String): Unit = println(prefix + name + postfix)
166+
```tut
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

173-
```
175+
```tut
174176
val greeter = new Greeter("Hello, ", "!")
175177
greeter.greet("Scala developer") // Hello, Scala developer!
176178
```
@@ -179,23 +181,23 @@ 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

184-
```
186+
```tut
185187
case class Point(x: Int, y: Int)
186188
```
187189

188190
You can instantiate case classes without `new` keyword.
189191

190-
```
192+
```tut
191193
val point = Point(1, 2)
192194
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

198-
```
200+
```tut
199201
if (point == anotherPoint) {
200202
println(point + " and " + anotherPoint + " are the same.")
201203
} else {
@@ -219,20 +221,19 @@ Objects are single instances of their own definitions. You can think of them as
219221

220222
You can define objects with the `object` keyword.
221223

222-
```
224+
```tut
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

235-
```
236+
```tut
236237
val newId: Int = IdFactory.create()
237238
println(newId) // 1
238239
val newerId: Int = IdFactory.create()
@@ -243,27 +244,28 @@ 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

250-
```
251+
```tut
251252
trait Greeter {
252253
def greet(name: String): Unit
253254
}
254255
```
255256

256257
Traits can also have default implementations.
257258

258-
```
259+
```tut
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

266-
```
268+
```tut
267269
class DefaultGreeter extends Greeter
268270
269271
class CustomizableGreeter(prefix: String, postfix: String) 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

290-
```
296+
```tut
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)