lesson 2 update

This commit is contained in:
Alexander Nozik 2021-03-19 18:39:14 +03:00
parent c5cba1d716
commit 78141cf98e
8 changed files with 43 additions and 14 deletions

View File

@ -1,5 +1,5 @@
plugins { plugins {
kotlin("jvm") version "1.4.30" kotlin("jvm") version "1.4.31"
} }
group = "ru.mipt.npm" group = "ru.mipt.npm"

View File

@ -8,11 +8,23 @@ import java.io.File
*/ */
fun idiom10() { fun idiom10() {
class A(val b: Boolean?) class A(val b: Boolean?)
val a: A = A(null)
val a: A? = A(null)
//use //use
a?.b == true a?.b == true
//instead of //instead of
a?.b ?: false a?.b ?: false
// The old way
val res = if (a == null) {
false
} else {
if (a.b == null) {
false
} else {
a.b
}
}
} }
/** /**

View File

@ -2,14 +2,16 @@ package lesson2
fun printNotNull(any: Any) = println(any) fun printNotNull(any: Any) = println(any)
val value: Int? = 2
fun main() { fun main() {
var value: Int? = 2
if (value != null) { if (value != null) {
//not guaranteed to work with mutable variable //not guaranteed to work with mutable variable
printNotNull(value) printNotNull(value)
} }
// Safe call here
value?.let { value?.let {
printNotNull(it) // execute this block if not null printNotNull(it) // execute this block if not null
printNotNull(value) // value is not null here printNotNull(value) // value is not null here

View File

@ -5,9 +5,9 @@ package lesson2
*/ */
fun String.countOs(): Int = count { it == 'о' } // implicit this points to String fun String.countOs(): Int = count { it == 'о' } // implicit this points to String
fun main() {
fun Int.printMe() = println(this) // explicit this fun Int.printMe() = println(this) // explicit this
fun main() {
"обороноспособность".countOs().printMe() "обороноспособность".countOs().printMe()
} }

View File

@ -3,10 +3,11 @@ package lesson2
//Interfaces and objects //Interfaces and objects
interface AnInterface { interface AnInterface {
val a: Int get() = 4
fun doSomething() fun doSomething()
} }
class AClass : AnInterface { class AClass(override val a: Int) : AnInterface {
override fun doSomething() = TODO() override fun doSomething() = TODO()
} }
@ -21,10 +22,10 @@ fun main() {
/** /**
* Creating an instance * 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 val obj = AnObject
@ -41,4 +42,6 @@ fun main() {
val voldemort = object { val voldemort = object {
fun doSomething(): Unit = TODO() fun doSomething(): Unit = TODO()
} }
voldemort.doSomething()
} }

View File

@ -9,3 +9,7 @@ fun checkType(arg: Any) = when(arg){
is Double -> println("I am a Double") is Double -> println("I am a Double")
else -> println("I don't know who am I?") else -> println("I don't know who am I?")
} }
fun main() {
checkType(true)
}

View File

@ -1,11 +1,17 @@
package lesson2 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() { fun main() {
val simple = SimpleClass(1,2.0,3.0)
println(simple)
val data = DataClass(2, 2.0) val data = DataClass(2, 2.0)
val copy = data.copy(b = 22.2) val copy = data.copy(b = 22.2)
println(copy) println(copy)

View File

@ -1,5 +1,7 @@
package lesson2 package lesson2
import lesson1.returnUnit
/** /**
* The declaration if valid because [TODO] returns Nothing * The declaration if valid because [TODO] returns Nothing
*/ */