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
+20-4Lines changed: 20 additions & 4 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
@@ -136,7 +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.
144
+
Notice how the return type is declared _after_ the parameter list: `: Int`. Methods cannot be named with the `val` or `var` keywords.
140
145
141
146
```tut:nofail
142
147
def add(x: Int, y: Int): Int = x + y
@@ -160,6 +165,16 @@ println("Hello, " + name + "!")
160
165
161
166
There are some other differences, but for now, you can think of them as something similar to functions.
162
167
168
+
Method can have multi-line expressions as well.
169
+
```tut
170
+
def getSquareString(input: Double): String = {
171
+
val square = input * input
172
+
square.toString
173
+
}
174
+
```
175
+
You can use the `return` but it is not necessary or idiomatic. Expressions
176
+
177
+
163
178
## Classes
164
179
165
180
You can define classes with the `class` keyword followed by its name and constructor parameters.
@@ -170,6 +185,7 @@ class Greeter(prefix: String, suffix: String) {
170
185
println(prefix + name + suffix)
171
186
}
172
187
```
188
+
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 but the main difference is that there is actually a singleton value of type `Unit`, written `()`.
173
189
174
190
You can make an instance of a class with the `new` keyword.
0 commit comments