Skip to content

Commit 587c8f1

Browse files
committed
small changes to basics
1 parent 7ab45eb commit 587c8f1

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

tutorials/tour/_posts/2017-02-13-basics.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ This is an easy, zero-setup way to experiment with pieces of Scala code.
2626
## Expressions
2727

2828
Expressions are computable statements.
29-
29+
```
30+
1 + 1
31+
```
3032
You can output results of expressions using `println`.
3133

3234
```tut
@@ -61,6 +63,8 @@ Types of values can be inferred, but you can also explicitly state the type, lik
6163
val x: Int = 1 + 1
6264
```
6365

66+
Notice how the type declaration `Int` comes after the identifier `x`. You also need a `:`.
67+
6468
### Variables
6569

6670
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:
7781
var x: Int = 1 + 1
7882
```
7983

80-
### Blocks
84+
85+
## Blocks
8186

8287
You can combine expressions by surrounding them with `{}`. We call this a block.
8388

@@ -94,7 +99,7 @@ println({
9499

95100
Functions are expressions that take parameters.
96101

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:
98103

99104
```tut
100105
(x: Int) => x + 1
@@ -136,7 +141,7 @@ def add(x: Int, y: Int): Int = x + y
136141
println(add(1, 2)) // 3
137142
```
138143

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.
140145

141146
```tut:nofail
142147
def add(x: Int, y: Int): Int = x + y
@@ -160,6 +165,16 @@ println("Hello, " + name + "!")
160165

161166
There are some other differences, but for now, you can think of them as something similar to functions.
162167

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+
163178
## Classes
164179

165180
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) {
170185
println(prefix + name + suffix)
171186
}
172187
```
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 `()`.
173189

174190
You can make an instance of a class with the `new` keyword.
175191

0 commit comments

Comments
 (0)