-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.kt
61 lines (51 loc) · 1.11 KB
/
Main.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 1 type declaration
class Person(val name: String, var age: Int) {
// 7 create function for type
fun haveBirthday() {
// 6 change instance field
this.age += 1
}
}
// 2 tagged union declaration
sealed class User {
class LoggedIn(val name: String) : User()
class Guest() : User()
}
// There are no top level expressions, main is needed
fun main(args: Array<String>) {
// 3 hello world
println("Hello World!")
// 4 creating instance of type
val person = Person(name = "Bob", age = 42)
// 5 call function
person.haveBirthday()
}
fun controlFlow(){
// 8 loop
for (i in 1..10) {
println(i)
}
// 9 if
if (true) {
print("true")
}
}
// 10 Switch
fun switch(user: User) {
when (user) {
is User.Guest -> println("Guest")
is User.LoggedIn -> println(user.name)
}
}
// 11 list
fun collections() {
val list = mutableListOf(2, 3)
list.add(4)
// 12 map
val map = mutableMapOf(1 to "a", 2 to "b")
map[3] = "c"
val d = map[4]
if (d != null) {
println(d) // Smart cast to not null
}
}