From c5cba1d7163157ce8da4ea012e0e7443d000a30a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 18 Mar 2021 16:00:05 +0300 Subject: [PATCH] lesson2 --- src/main/kotlin/lesson2/idiom14-15.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/lesson2/idiom14-15.kt b/src/main/kotlin/lesson2/idiom14-15.kt index 38b1493..c73c8fa 100644 --- a/src/main/kotlin/lesson2/idiom14-15.kt +++ b/src/main/kotlin/lesson2/idiom14-15.kt @@ -1,9 +1,26 @@ package lesson2 +/** + * Extension property on a String. It is not monkey-patching. + */ fun String.countOs(): Int = count { it == 'о' } // implicit this points to String fun Int.printMe() = println(this) // explicit this fun main() { "обороноспособность".countOs().printMe() -} \ No newline at end of file +} + +/** + * Extension property (must be virtual) + */ +val List.odd get() = filter { it % 2 == 1 } + +/** + * Extension variable (also must be virtual) + */ +var Array.second: Int + get() = get(1) + set(value) { + set(1, value) + } \ No newline at end of file