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
You can name results of expressions by using the `val` keyword.
40
+
You can name results of expressions with the `val` keyword.
41
41
42
-
```
42
+
```tut
43
43
val x = 1 + 1
44
44
println(x) // 2
45
45
```
46
46
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.
48
49
49
50
Values cannot be re-assigned.
50
51
51
-
```
52
+
```tut:nofail
52
53
val x = 1 + 1
53
54
x = 3 // This does not compile.
54
55
```
55
56
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:
57
58
58
-
```
59
+
```tut
59
60
val x: Int = 1 + 1
60
61
```
61
62
62
63
### Variables
63
64
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.
65
66
66
-
```
67
+
```tut
67
68
var x = 1 + 1
68
69
x = 3 // This compiles because "x" is declared with the "var" keyword.
69
-
println(x) // 3
70
+
println(x * x) // 9
70
71
```
71
72
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:
73
74
74
-
```
75
+
```tut
75
76
var x: Int = 1 + 1
76
77
```
77
78
78
79
### Blocks
79
80
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.
81
82
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.
83
84
84
-
```
85
+
```tut
85
86
println({
86
87
val x = 1 + 1
87
88
x + 1
@@ -90,33 +91,33 @@ println({
90
91
91
92
## Functions
92
93
93
-
Functions are expressions with parameters.
94
+
Functions are expressions that take parameters.
94
95
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:
96
97
97
-
```
98
+
```tut
98
99
(x: Int) => x + 1
99
100
```
100
101
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.
102
103
103
104
You can also name functions.
104
105
105
-
```
106
+
```tut
106
107
val addOne = (x: Int) => x + 1
107
108
println(addOne(1)) // 2
108
109
```
109
110
110
-
Or it can take multiple parameters.
111
+
Functions may take multiple parameters.
111
112
112
-
```
113
+
```tut
113
114
val add = (x: Int, y: Int) => x + y
114
115
println(add(1, 2)) // 3
115
116
```
116
117
117
118
Or it can take no parameters.
118
119
119
-
```
120
+
```tut
120
121
val getTheAnswer = () => 42
121
122
println(getTheAnswer()) // 42
122
123
```
@@ -127,31 +128,31 @@ We will cover functions in depth [later](anonymous-function-syntax.md).
127
128
128
129
Methods look and behave very similar to functions, but there are a few key differences between them.
129
130
130
-
Methods are defined like below with the `def` keywordfollowed 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.
131
132
132
-
```
133
+
```tut
133
134
def add(x: Int, y: Int): Int = x + y
134
135
println(add(1, 2)) // 3
135
136
```
136
137
137
138
Methods cannot be named with the `val` or `var` keywords.
@@ -179,23 +181,23 @@ We will cover classes in depth [later](classes.md).
179
181
180
182
## Case Classes
181
183
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.
183
185
184
-
```
186
+
```tut
185
187
case class Point(x: Int, y: Int)
186
188
```
187
189
188
190
You can instantiate case classes without `new` keyword.
189
191
190
-
```
192
+
```tut
191
193
val point = Point(1, 2)
192
194
val anotherPoint = Point(1, 2)
193
195
val yetAnotherPoint = Point(2, 2)
194
196
```
195
197
196
-
And they are compared by values.
198
+
And they are compared by value.
197
199
198
-
```
200
+
```tut
199
201
if (point == anotherPoint) {
200
202
println(point + " and " + anotherPoint + " are the same.")
201
203
} else {
@@ -219,20 +221,19 @@ Objects are single instances of their own definitions. You can think of them as
219
221
220
222
You can define objects with the `object` keyword.
221
223
222
-
```
224
+
```tut
223
225
object IdFactory {
224
226
private var counter = 0
225
-
226
227
def create(): Int = {
227
228
counter += 1
228
229
counter
229
230
}
230
231
}
231
232
```
232
233
233
-
You can access objects by referring its name.
234
+
You can access an object by referring to its name.
234
235
235
-
```
236
+
```tut
236
237
val newId: Int = IdFactory.create()
237
238
println(newId) // 1
238
239
val newerId: Int = IdFactory.create()
@@ -243,27 +244,28 @@ We will cover objects in depth [later](singleton-objects.md).
243
244
244
245
## Traits
245
246
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.
247
248
248
249
You can define traits with `trait` keyword.
249
250
250
-
```
251
+
```tut
251
252
trait Greeter {
252
253
def greet(name: String): Unit
253
254
}
254
255
```
255
256
256
257
Traits can also have default implementations.
257
258
258
-
```
259
+
```tut
259
260
trait Greeter {
260
-
def greet(name: String): Unit = println("Hello, " + name + "!")
261
+
def greet(name: String): Unit =
262
+
println("Hello, " + name + "!")
261
263
}
262
264
```
263
265
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.
265
267
266
-
```
268
+
```tut
267
269
class DefaultGreeter extends Greeter
268
270
269
271
class CustomizableGreeter(prefix: String, postfix: String) extends Greeter {
@@ -279,16 +281,21 @@ val customGreeter = new CustomizableGreeter("How are you, ", "?")
279
281
customGreeter.greet("Scala developer") // How are you, Scala developer?
280
282
```
281
283
284
+
Here, `DefaultGreeter` extends only a single trait, but it could extend multiple traits.
285
+
282
286
We will cover traits in depth [later](traits.md).
283
287
284
288
## Main Method
285
289
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.
287
293
288
-
Using objects, you can define main methods like below.
294
+
Using an object, you can define a main method as follows:
289
295
290
-
```
296
+
```tut
291
297
object Main {
292
-
def main(args: Array[String]): Unit = println("Hello, Scala developer!")
0 commit comments