This commit is contained in:
Alexander Nozik 2021-03-18 16:00:05 +03:00
parent d7b5dfdb80
commit c5cba1d716

View File

@ -1,5 +1,8 @@
package lesson2 package lesson2
/**
* Extension property on a String. It is not monkey-patching.
*/
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 Int.printMe() = println(this) // explicit this
@ -7,3 +10,17 @@ fun Int.printMe() = println(this) // explicit this
fun main() { fun main() {
"обороноспособность".countOs().printMe() "обороноспособность".countOs().printMe()
} }
/**
* Extension property (must be virtual)
*/
val List<Int>.odd get() = filter { it % 2 == 1 }
/**
* Extension variable (also must be virtual)
*/
var Array<Int>.second: Int
get() = get(1)
set(value) {
set(1, value)
}