Skip to content

Commit 0cdd508

Browse files
authored
Merge pull request #751 from travissarles/basics
small changes to basics
2 parents 1a5aabb + 0a248a4 commit 0cdd508

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

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

Lines changed: 24 additions & 15 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
@@ -123,7 +128,7 @@ val getTheAnswer = () => 42
123128
println(getTheAnswer()) // 42
124129
```
125130

126-
We will cover functions in depth [later](anonymous-function-syntax.md).
131+
We will cover functions in depth [later](anonymous-function-syntax.html).
127132

128133
## Methods
129134

@@ -136,13 +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.
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`.
146145

147146
Methods can take multiple parameter lists.
148147

@@ -160,6 +159,15 @@ println("Hello, " + name + "!")
160159

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

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+
163171
## Classes
164172

165173
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) {
170178
println(prefix + name + suffix)
171179
}
172180
```
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.)
173182

174183
You can make an instance of a class with the `new` keyword.
175184

@@ -178,7 +187,7 @@ val greeter = new Greeter("Hello, ", "!")
178187
greeter.greet("Scala developer") // Hello, Scala developer!
179188
```
180189

181-
We will cover classes in depth [later](classes.md).
190+
We will cover classes in depth [later](classes.html).
182191

183192
## Case Classes
184193

@@ -214,7 +223,7 @@ if (point == yetAnotherPoint) {
214223
// Point(1,2) and Point(2,2) are different.
215224
```
216225

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).
218227

219228
## Objects
220229

@@ -241,7 +250,7 @@ val newerId: Int = IdFactory.create()
241250
println(newerId) // 2
242251
```
243252

244-
We will cover objects in depth [later](singleton-objects.md).
253+
We will cover objects in depth [later](singleton-objects.html).
245254

246255
## Traits
247256

@@ -284,7 +293,7 @@ customGreeter.greet("Scala developer") // How are you, Scala developer?
284293

285294
Here, `DefaultGreeter` extends only a single trait, but it could extend multiple traits.
286295

287-
We will cover traits in depth [later](traits.md).
296+
We will cover traits in depth [later](traits.html).
288297

289298
## Main Method
290299

0 commit comments

Comments
 (0)