lesson 2 update
This commit is contained in:
parent
c5cba1d716
commit
78141cf98e
@ -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"
|
||||||
|
@ -8,17 +8,29 @@ 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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dart-like (?=) nullable assignment
|
* Dart-like (?=) nullable assignment
|
||||||
*/
|
*/
|
||||||
fun idiom11(){
|
fun idiom11() {
|
||||||
var x: String? = null
|
var x: String? = null
|
||||||
x = x ?: "Hello"
|
x = x ?: "Hello"
|
||||||
}
|
}
|
||||||
@ -26,7 +38,7 @@ fun idiom11(){
|
|||||||
/**
|
/**
|
||||||
* Safe call and elvis operator
|
* Safe call and elvis operator
|
||||||
*/
|
*/
|
||||||
fun idiom12(){
|
fun idiom12() {
|
||||||
val files = File("Test").listFiles()
|
val files = File("Test").listFiles()
|
||||||
println(files?.size ?: "empty")
|
println(files?.size ?: "empty")
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,16 @@ package lesson2
|
|||||||
|
|
||||||
fun printNotNull(any: Any) = println(any)
|
fun printNotNull(any: Any) = println(any)
|
||||||
|
|
||||||
fun main(){
|
val value: Int? = 2
|
||||||
var value: Int? = 2
|
|
||||||
|
|
||||||
if(value != null){
|
fun main() {
|
||||||
|
|
||||||
|
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
|
||||||
|
@ -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 Int.printMe() = println(this) // explicit this
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
|
fun Int.printMe() = println(this) // explicit this
|
||||||
|
|
||||||
"обороноспособность".countOs().printMe()
|
"обороноспособность".countOs().printMe()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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()
|
||||||
}
|
}
|
@ -8,4 +8,8 @@ fun checkType(arg: Any) = when(arg){
|
|||||||
is Int -> println("I am an Int")
|
is Int -> println("I am an Int")
|
||||||
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)
|
||||||
}
|
}
|
@ -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)
|
||||||
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user