From 78141cf98ee6546d676e581fb09a899f2cce6329 Mon Sep 17 00:00:00 2001 From: darksnake Date: Fri, 19 Mar 2021 18:39:14 +0300 Subject: [PATCH] lesson 2 update --- build.gradle.kts | 2 +- src/main/kotlin/lesson2/idiom10-12.kt | 18 +++++++++++++++--- src/main/kotlin/lesson2/idiom13.kt | 8 +++++--- src/main/kotlin/lesson2/idiom14-15.kt | 4 ++-- src/main/kotlin/lesson2/idiom6.kt | 9 ++++++--- src/main/kotlin/lesson2/idiom7.kt | 4 ++++ src/main/kotlin/lesson2/idiom8.kt | 10 ++++++++-- src/main/kotlin/lesson2/idiom9.kt | 2 ++ 8 files changed, 43 insertions(+), 14 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index bb10686..a321a2f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - kotlin("jvm") version "1.4.30" + kotlin("jvm") version "1.4.31" } group = "ru.mipt.npm" diff --git a/src/main/kotlin/lesson2/idiom10-12.kt b/src/main/kotlin/lesson2/idiom10-12.kt index 8f79b2c..5358fdf 100644 --- a/src/main/kotlin/lesson2/idiom10-12.kt +++ b/src/main/kotlin/lesson2/idiom10-12.kt @@ -8,17 +8,29 @@ import java.io.File */ fun idiom10() { class A(val b: Boolean?) - val a: A = A(null) + + val a: A? = A(null) //use a?.b == true //instead of a?.b ?: false + + // The old way + val res = if (a == null) { + false + } else { + if (a.b == null) { + false + } else { + a.b + } + } } /** * Dart-like (?=) nullable assignment */ -fun idiom11(){ +fun idiom11() { var x: String? = null x = x ?: "Hello" } @@ -26,7 +38,7 @@ fun idiom11(){ /** * Safe call and elvis operator */ -fun idiom12(){ +fun idiom12() { val files = File("Test").listFiles() println(files?.size ?: "empty") } diff --git a/src/main/kotlin/lesson2/idiom13.kt b/src/main/kotlin/lesson2/idiom13.kt index 20bd06b..dff6625 100644 --- a/src/main/kotlin/lesson2/idiom13.kt +++ b/src/main/kotlin/lesson2/idiom13.kt @@ -2,14 +2,16 @@ package lesson2 fun printNotNull(any: Any) = println(any) -fun main(){ - var value: Int? = 2 +val value: Int? = 2 - if(value != null){ +fun main() { + + if (value != null) { //not guaranteed to work with mutable variable printNotNull(value) } + // Safe call here value?.let { printNotNull(it) // execute this block if not null printNotNull(value) // value is not null here diff --git a/src/main/kotlin/lesson2/idiom14-15.kt b/src/main/kotlin/lesson2/idiom14-15.kt index c73c8fa..a7255b6 100644 --- a/src/main/kotlin/lesson2/idiom14-15.kt +++ b/src/main/kotlin/lesson2/idiom14-15.kt @@ -5,9 +5,9 @@ package lesson2 */ fun String.countOs(): Int = count { it == 'о' } // implicit this points to String -fun Int.printMe() = println(this) // explicit this - fun main() { + fun Int.printMe() = println(this) // explicit this + "обороноспособность".countOs().printMe() } diff --git a/src/main/kotlin/lesson2/idiom6.kt b/src/main/kotlin/lesson2/idiom6.kt index 547a091..9cb4a61 100644 --- a/src/main/kotlin/lesson2/idiom6.kt +++ b/src/main/kotlin/lesson2/idiom6.kt @@ -3,10 +3,11 @@ package lesson2 //Interfaces and objects interface AnInterface { + val a: Int get() = 4 fun doSomething() } -class AClass : AnInterface { +class AClass(override val a: Int) : AnInterface { override fun doSomething() = TODO() } @@ -21,10 +22,10 @@ fun main() { /** * Creating an instance */ - val instance = AClass() + val instance = AClass(3) /** - * Using singleton reference without constructor invokation + * Using singleton reference without constructor invocation */ val obj = AnObject @@ -41,4 +42,6 @@ fun main() { val voldemort = object { fun doSomething(): Unit = TODO() } + + voldemort.doSomething() } \ No newline at end of file diff --git a/src/main/kotlin/lesson2/idiom7.kt b/src/main/kotlin/lesson2/idiom7.kt index 77199bb..f0d85b5 100644 --- a/src/main/kotlin/lesson2/idiom7.kt +++ b/src/main/kotlin/lesson2/idiom7.kt @@ -8,4 +8,8 @@ fun checkType(arg: Any) = when(arg){ is Int -> println("I am an Int") is Double -> println("I am a Double") else -> println("I don't know who am I?") +} + +fun main() { + checkType(true) } \ No newline at end of file diff --git a/src/main/kotlin/lesson2/idiom8.kt b/src/main/kotlin/lesson2/idiom8.kt index 252ad23..b7e8d48 100644 --- a/src/main/kotlin/lesson2/idiom8.kt +++ b/src/main/kotlin/lesson2/idiom8.kt @@ -1,11 +1,17 @@ package lesson2 -class SimpleClass(val a: Int, val b: Double) +class SimpleClass(val a: Int, val b: Double, c: Double) -data class DataClass(val a: Int, val b: Double) +data class DataClass(val a: Int, val b: Double){ + val c get() = b +1 +} fun main() { + val simple = SimpleClass(1,2.0,3.0) + + println(simple) + val data = DataClass(2, 2.0) val copy = data.copy(b = 22.2) println(copy) diff --git a/src/main/kotlin/lesson2/idiom9.kt b/src/main/kotlin/lesson2/idiom9.kt index f59b797..72c66cc 100644 --- a/src/main/kotlin/lesson2/idiom9.kt +++ b/src/main/kotlin/lesson2/idiom9.kt @@ -1,5 +1,7 @@ package lesson2 +import lesson1.returnUnit + /** * The declaration if valid because [TODO] returns Nothing */