8000 Update basics.md · soronpo/scala.github.com@10ca450 · GitHub
[go: up one dir, main page]

Skip to content

Commit 10ca450

Browse files
authored
Update basics.md
1 parent 37d857b commit 10ca450

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

tutorials/tour/basics.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,28 @@ next-page: unified-types
1010
previous-page: tour-of-scala
1111
---
1212

13-
## Values and Variables
13+
In this page, we will cover basics of Scala.
1414

15-
You can define values with `val` keyword.
15+
## Variables
16+
17+
You can define variables with `var` keyword.
1618

1719
```
18-
val x = 1 + 1
20+
var x = 1 + 1
21+
x += 1
1922
```
2023

21-
When defined with `val`, you cannot mutate the binding.
24+
Often, you want your variables to be immutable. You can use `val` keyword in that case.
2225

2326
```
2427
val x = 1 + 1
25-
x += 1 // This does not compile because you are mutating values defined with "val" keyword
28+
x += 1 // This does not compile because you declared the variable with "val" keyword
2629
```
2730

28-
You can use `var` instead to make it variable, allowing mutation.
31+
Type of variables can be inferred, but you can also explicitly state type like below.
2932

3033
```
31-
var x = 1 + 1
32-
x += 1
34+
val x: Int = 1 + 1
3335
```
3436

3537
## Functions
@@ -75,7 +77,7 @@ We will cover classes in depth [later](classes.md).
7577

7678
## Traits
7779

78-
Traits are used to define object types by specifying the signature of fields and methods.
80+
Traits define types as signature of fields and methods.
7981

8082
You can define traits with `trait` keyword.
8183

@@ -146,7 +148,7 @@ println(point == yetAnotherPoint) // false
146148

147149
Case classes are immutable by default, but it also provides `copy` method so that you can easily create another instance of the class while reusing the values from exiting instances.
148150

149-
Using `copy` method, you can write the above code like below.
151+
Using `copy` method, you can also write the above code like below.
150152

151153
```
152154
val point = Point(1, 2)
@@ -156,7 +158,7 @@ println(point == anotherPoint) // true
156158
println(point == yetAnotherPoint) // false
157159
```
158160

159-
There are many other features you get out-of-box by using case classes. We will cover them [later](case-classes.md).
161+
There are many other features you get out-of-the-box by using case classes. We will cover them in depth [later](case-classes.md).
160162

161163
## Singleton Objects
162164

@@ -173,6 +175,15 @@ object IdFactory {
173175
}
174176
```
175177

178+
You can access singleton objects just by referring its name.
179+
180+
```
181+
val newId: Int = IdFactory.create()
182+
println(newId) // 1
183+
val newerId: Int = IdFactory.create()
184+
println(newerId) // 2
185+
```
186+
176187
We will cover singleton objects in depth [later](singleton-objects.md).
177188

178189
## Main Method

0 commit comments

Comments
 (0)
0