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
Copy file name to clipboardExpand all lines: tutorials/tour/_posts/2017-02-13-basics.md
+24-15Lines changed: 24 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,9 @@ This is an easy, zero-setup way to experiment with pieces of Scala code.
26
26
## Expressions
27
27
28
28
Expressions are computable statements.
29
-
29
+
```
30
+
1 + 1
31
+
```
30
32
You can output results of expressions using `println`.
31
33
32
34
```tut
@@ -61,6 +63,8 @@ Types of values can be inferred, but you can also explicitly state the type, lik
61
63
val x: Int = 1 + 1
62
64
```
63
65
66
+
Notice how the type declaration `Int` comes after the identifier `x`. You also need a `:`.
67
+
64
68
### Variables
65
69
66
70
Variables are like values, except you can re-assign them. You can define a variable with the `var` keyword.
@@ -77,7 +81,8 @@ As with values, you can explicitly state the type if you want:
77
81
var x: Int = 1 + 1
78
82
```
79
83
80
-
### Blocks
84
+
85
+
## Blocks
81
86
82
87
You can combine expressions by surrounding them with `{}`. We call this a block.
83
88
@@ -94,7 +99,7 @@ println({
94
99
95
100
Functions are expressions that take parameters.
96
101
97
-
You can define a function that returns a given integer plus one:
102
+
You can define an anonymous function (i.e. no name) that returns a given integer plus one:
98
103
99
104
```tut
100
105
(x: Int) => x + 1
@@ -123,7 +128,7 @@ val getTheAnswer = () => 42
123
128
println(getTheAnswer()) // 42
124
129
```
125
130
126
-
We will cover functions in depth [later](anonymous-function-syntax.md).
131
+
We will cover functions in depth [later](anonymous-function-syntax.html).
127
132
128
133
## Methods
129
134
@@ -136,13 +141,7 @@ def add(x: Int, y: Int): Int = x + y
136
141
println(add(1, 2)) // 3
137
142
```
138
143
139
-
Methods cannot be named with the `val` or `var` keywords.
140
-
141
-
```tut:nofail
142
-
def add(x: Int, y: Int): Int = x + y
143
-
val add2 = add // This does not compile.
144
-
var add3 = add // This does not compile either.
145
-
```
144
+
Notice how the return type is declared _after_ the parameter list and a colon `: Int`.
146
145
147
146
Methods can take multiple parameter lists.
148
147
@@ -160,6 +159,15 @@ println("Hello, " + name + "!")
160
159
161
160
There are some other differences, but for now, you can think of them as something similar to functions.
162
161
162
+
Methods can have multi-line expressions as well.
163
+
```tut
164
+
def getSquareString(input: Double): String = {
165
+
val square = input * input
166
+
square.toString
167
+
}
168
+
```
169
+
The last expression in the body is the method's return value. (Scala does have a `return` keyword, but it's rarely used.)
170
+
163
171
## Classes
164
172
165
173
You can define classes with the `class` keyword followed by its name and constructor parameters.
@@ -170,6 +178,7 @@ class Greeter(prefix: String, suffix: String) {
170
178
println(prefix + name + suffix)
171
179
}
172
180
```
181
+
The return type of the method `greet` is `Unit`, which says there's nothing meaningful to return. It's used similarly to `void` in Java and C. (A difference is that because every Scala expression must have some value, there is actually a singleton value of type Unit, written (). It carries no information.)
173
182
174
183
You can make an instance of a class with the `new` keyword.
175
184
@@ -178,7 +187,7 @@ val greeter = new Greeter("Hello, ", "!")
We will cover classes in depth [later](classes.md).
190
+
We will cover classes in depth [later](classes.html).
182
191
183
192
## Case Classes
184
193
@@ -214,7 +223,7 @@ if (point == yetAnotherPoint) {
214
223
// Point(1,2) and Point(2,2) are different.
215
224
```
216
225
217
-
There is a lot more to case classes that we'd like to introduce, and we are convinced you will fall in love with them! We will cover them in depth [later](case-classes.md).
226
+
There is a lot more to case classes that we'd like to introduce, and we are convinced you will fall in love with them! We will cover them in depth [later](case-classes.html).
218
227
219
228
## Objects
220
229
@@ -241,7 +250,7 @@ val newerId: Int = IdFactory.create()
241
250
println(newerId) // 2
242
251
```
243
252
244
-
We will cover objects in depth [later](singleton-objects.md).
253
+
We will cover objects in depth [later](singleton-objects.html).
245
254
246
255
## Traits
247
256
@@ -284,7 +293,7 @@ customGreeter.greet("Scala developer") // How are you, Scala developer?
284
293
285
294
Here, `DefaultGreeter` extends only a single trait, but it could extend multiple traits.
286
295
287
-
We will cover traits in depth [later](traits.md).
296
+
We will cover traits in depth [later](traits.html).
0 commit comments