Skip to content

Commit a464dc9

Browse files
chore: update for Kotlin 1.3
1 parent 610367c commit a464dc9

File tree

13 files changed

+67
-24
lines changed

13 files changed

+67
-24
lines changed

examples/01_introduction/01_Hello world.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Hello world
22

33
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
4+
45
```kotlin
56
package org.kotlinlang.play // 1
67

78
fun main() { // 2
89
println("Hello, World!") // 3
910
}
1011
```
12+
1113
</div>
1214

1315
1. Kotlin code is usually defined in packages. If you don't define one, the default package will be used.
@@ -18,10 +20,12 @@ also, note that semicolons are optional.
1820

1921
For Kotlin versions less than 1.3 `main` function must be defined with parameter:
2022

21-
<div class="language-kotlin" theme="idea">
23+
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.2">
24+
2225
```kotlin
2326
fun main(args: Array<String>) {
2427
println("Hello, World!")
2528
}
2629
```
30+
2731
</div>

examples/01_introduction/02_String Templates.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# String Templates
22

33
<div class="language-kotlin" theme="idea">
4+
45
```kotlin
5-
fun main(args: Array<String>) {
6+
fun main() {
67
//sampleStart
78
val greeting = "Kotliner"
89

examples/01_introduction/03_Variables.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ compiler do the work by inferring it. While Kotlin does not enforce immutability
55
In essence use *val* over *var*.
66

77
<div class="language-kotlin" theme="idea">
8+
89
```kotlin
9-
fun main(args: Array<String>) {
10+
fun main() {
1011
//sampleStart
1112
var a: String = "initial" // 1
1213
println(a)
@@ -15,21 +16,24 @@ fun main(args: Array<String>) {
1516
//sampleEnd
1617
}
1718
```
19+
1820
</div>
1921

2022
1. Declare a mutable variable and initialise it
2123
2. Declare an immutable variable and initialise it
2224
3. Declare an immutable variable and initialise it. The compiler infers the type.
2325

2426
<div class="language-kotlin" theme="idea">
27+
2528
```kotlin
26-
fun main(args: Array<String>) {
29+
fun main() {
2730
//sampleStart
2831
var e: Int // 1
2932
println(e) // 2
3033
//sampleEnd
3134
}
3235
```
36+
3337
</div>
3438

3539
1. Declare a variable, but don't initialise it.
@@ -38,10 +42,11 @@ fun main(args: Array<String>) {
3842
Variable initialization can be postponed, but it must be initialized before the first read.
3943

4044
<div class="language-kotlin" theme="idea">
45+
4146
```kotlin
4247
fun someCondition() = true
4348

44-
fun main(args: Array<String>) {
49+
fun main() {
4550
//sampleStart
4651
val d: Int // 1
4752

@@ -55,6 +60,7 @@ fun main(args: Array<String>) {
5560
//sampleEnd
5661
}
5762
```
63+
5864
</div>
5965

6066
1. Declare a variable, but don't initialise it.

examples/01_introduction/04_Functions.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
### Default parameter values and named arguments
44

5-
<div class="language-kotlin" theme="idea" theme="idea">
5+
<div class="language-kotlin" theme="idea">
6+
67
```kotlin
78
fun printMessage(message: String): Unit { // 1
89
println(message)
@@ -18,14 +19,15 @@ fun sum(x: Int, y: Int): Int { // 3
1819

1920
fun multiply(x: Int, y: Int) = x * y // 4
2021

21-
fun main(args: Array<String>) {
22+
fun main() {
2223
printMessage("Hello") // 5
2324
printMessageWithPrefix("Hello", "Log") // 6
2425
printMessageWithPrefix("Hello") // 7
2526
printMessageWithPrefix(prefix = "Log", message = "Hello") // 8
2627
sum(1, 2)
2728
}
2829
```
30+
2931
</div>
3032

3133
1. Create a function that takes a parameter of type string and returns Unit (i.e. no return value).
@@ -41,7 +43,8 @@ fun main(args: Array<String>) {
4143

4244
Member functions and extensions with a single parameter can be turned into infix functions.
4345

44-
<div class="language-kotlin" theme="idea" theme="idea">
46+
<div class="language-kotlin" theme="idea">
47+
4548
```kotlin
4649
fun main(args: Array<String>) {
4750

@@ -65,6 +68,7 @@ class Person(val name: String) {
6568
infix fun likes(other: Person) { likedPeople.add(other) } // 6
6669
}
6770
```
71+
6872
</div>
6973

7074
1. Defines an infix extension function on `Int`
@@ -80,9 +84,10 @@ Note that the example uses _local functions_ (functions nested into another func
8084

8185
Certain functions can be "upgraded" to operators, allowing to use them with the corresponding operator symbol.
8286

83-
<div class="language-kotlin" theme="idea" theme="idea">
87+
<div class="language-kotlin" theme="idea">
88+
8489
```kotlin
85-
fun main(args: Array<String>) {
90+
fun main() {
8691
//sampleStart
8792

8893
operator fun Int.times(str: String) = str.repeat(this) // 1
@@ -94,6 +99,7 @@ fun main(args: Array<String>) {
9499
//sampleEnd
95100
}
96101
```
102+
97103
</div>
98104

99105
1. This takes the infix function from above one step further using the `operator` modifier.
@@ -105,9 +111,10 @@ fun main(args: Array<String>) {
105111

106112
Varargs allow passing any number of arguments by comma-separating them.
107113

108-
<div class="language-kotlin" theme="idea" theme="idea">
114+
<div class="language-kotlin" theme="idea">
115+
109116
```kotlin
110-
fun main(args: Array<String>) {
117+
fun main() {
111118
//sampleStart
112119
fun printAll(vararg messages: String) { // 1
113120
for (m in messages) println(m)
@@ -126,6 +133,7 @@ fun main(args: Array<String>) {
126133
//sampleEnd
127134
}
128135
```
136+
129137
</div>
130138

131139
1. The `vararg` modifier turns a parameter into a vararg.

examples/01_introduction/05_Null Safety.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ In an effort to rid the world of `NullPointerException`, variable types in Kotli
66

77
```kotlin
88
//sampleStart
9-
fun main(args: Array<String>) {
9+
fun main() {
1010
var neverNull: String = "This can't be null" // 1
1111

1212
var nullable: String? = "You can keep a null here" // 2
@@ -32,6 +32,7 @@ Sometimes Kotlin programs need to work with null values, such as when interactin
3232
representing a truly absent state. Kotlin provides null tracking to elegantly deal with such situations.
3333

3434
<div class="language-kotlin" theme="idea">
35+
3536
```kotlin
3637
//sampleStart
3738
fun describeString(maybeString: String?): String { // 1
@@ -43,10 +44,11 @@ fun describeString(maybeString: String?): String { // 1
4344
}
4445
//sampleEnd
4546

46-
fun main(args: Array<String>) {
47+
fun main() {
4748
println(describeString(null))
4849
}
4950
```
51+
5052
</div>
5153

5254

examples/02_advanced/01_namedArguments.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# Named Arguments
22

33
<div class="language-kotlin" theme="idea">
4+
45
```kotlin
56
fun format(userName: String, domain: String) = "$userName@$domain"
67

7-
fun main(args: Array<String>) {
8+
fun main() {
89
//sampleStart
910
println(format("mario", "example.com")) // 1
1011
println(format(userName = "foo", domain = "bar.com")) // 2
1112
println(format(domain = "frog.com", userName = "pepe")) // 3
1213
//sampleEnd
1314
}
1415
```
16+
1517
</div>
1618

1719
1. As most of programming languages(Java, C++ etc), Kotlin supports passing arguments to methods and constructors

examples/02_advanced/02_extensionFunctions.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Extension Functions and Properties
22

33
<div class="language-kotlin" theme="idea">
4+
45
```kotlin
56
data class Item(val name: String, val price: Float) // 1
67

@@ -12,7 +13,7 @@ fun Order.maxPricedItemName() = this.items.maxBy { it.price }?.name ?: "NO_PRODU
1213
val Order.commaDelimitedItemNames: String // 3
1314
get() = items.map { it.name }.joinToString()
1415

15-
fun main(args: Array<String>) {
16+
fun main() {
1617

1718
val order = Order(listOf(Item("Bread", 25.0F), Item("Wine", 29.0F), Item("Water", 12.0F)))
1819

@@ -22,6 +23,7 @@ fun main(args: Array<String>) {
2223

2324
}
2425
```
26+
2527
</div>
2628

2729
1. We define simple models of `Item` and `Order`. `Order` can contain arbitrary `Collection` of `Item` s
@@ -34,13 +36,15 @@ is very much like standard function or property signature, with addition that we
3436
It is even possible to execute extensions on `null` references. In their implementation we can check for `null` reference and based on that we can do any arbitrary logic. Example:
3537

3638
<div class="language-kotlin" theme="idea">
39+
3740
```kotlin
3841
//sampleStart
3942
fun <T> T?.nullSafeToString() = this?.toString() ?: "NULL" // 1
4043
//sampleEnd
41-
fun main(args: Array<String>) {
44+
fun main() {
4245
println(null.nullSafeToString())
4346
println("Kotlin".nullSafeToString())
4447
}
4548
```
49+
4650
</div>

examples/02_advanced/03_Data classes.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ Everything that's needed to use them in collections, have a useful string
55
representation, and create copies is auto-generated.
66

77
<div class="language-kotlin" theme="idea">
8+
89
```kotlin
910
data class User(val name: String, val id: Int)
1011

11-
fun main(args: Array<String>) {
12+
fun main() {
1213
val user = User("Alex", 1)
1314
println(user) // 1
1415

@@ -31,6 +32,7 @@ fun main(args: Array<String>) {
3132
println("id = ${user.component2}")
3233
}
3334
```
35+
3436
</div>
3537

3638
1. Method `toString` is auto-generated, which makes `println` output look nice.

examples/02_advanced/04_Destructuring Declarations.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
_Destructuring declaration_ syntax can be very handy and save you few lines of code.
44

5-
<div class="language-kotlin" theme="idea" theme="idea">
5+
<div class="language-kotlin" theme="idea">
6+
67
```kotlin
78
fun findMinMax(list: List<Int>): Pair<Int, Int> {
89
// do the math
910
return Pair(50, 100)
1011
}
1112

12-
fun main(args: Array<String>) {
13+
fun main() {
1314
//sampleStart
1415
val (x, y, z) = arrayOf(5, 10, 15) // 1
1516

@@ -23,19 +24,21 @@ fun main(args: Array<String>) {
2324
//sampleEnd
2425
}
2526
```
27+
2628
</div>
2729

2830
1. Create a component on the left-hand side to match the arity of the right hand
2931
2. It can be used to iterate through maps. _name_ and _age_ variables are mapped to key and value now
3032
3. You can destructure built-in Pairs and Triples, even as return values from functions
3133

32-
<div class="language-kotlin" theme="idea" theme="idea">
34+
<div class="language-kotlin" theme="idea">
35+
3336
```kotlin
3437
data class User(val username: String, val email: String) // 1
3538

3639
fun getUser() = User("Mary", "[email protected]")
3740

38-
fun main(args: Array<String>) {
41+
fun main() {
3942
val user = getUser()
4043
val (username, email) = user // 2
4144
println(username == user.component1()) // 3
@@ -44,14 +47,16 @@ fun main(args: Array<String>) {
4447

4548
}
4649
```
50+
4751
</div>
4852

4953
1. Define a data class that will be destructured later
5054
2. Values mapped to object fields
5155
3. Data class automatically defines the `component1()`, `component2()` corresponding methods, which will be called during destructuring
5256
4. Use _underscore_ if you don't need one of the values, avoiding the compiler hint indicating unused variable
5357

54-
<div class="language-kotlin" theme="idea" theme="idea">
58+
<div class="language-kotlin" theme="idea">
59+
5560
```kotlin
5661
class Pair<K, V>(val first: K, val second: V) { // 1
5762
operator fun component1(): K {
@@ -63,12 +68,13 @@ class Pair<K, V>(val first: K, val second: V) { // 1
6368
}
6469
}
6570

66-
fun main(args: Array<String>) {
71+
fun main() {
6772
val (num, name) = Pair(1, "one") // 2
6873

6974
println("num = $num, name = $name")
7075
}
7176
```
77+
7278
</div>
7379

7480
1. Define a custom `Pair` class with `component1()` and `component2()` methods

examples/03_stdlib/01_letFunction.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
**let** is a useful function defined in Kotlin Standard Library. It can be used for scoping and null-checks.
44

55
<div class="language-kotlin" theme="idea">
6+
67
```kotlin
78
import java.io.File
89

@@ -14,6 +15,7 @@ fun main(args: Array<String>) {
1415
//sampleEnd
1516
}
1617
```
18+
1719
</div>
1820

1921

0 commit comments

Comments
 (0)