Added delegated property through map support + tests.

This commit is contained in:
Igor Chevdar
2017-02-28 15:44:06 +03:00
parent 889b28ee87
commit e2f2e31dc1
6 changed files with 183 additions and 0 deletions
@@ -0,0 +1,9 @@
val lazyValue: String by lazy {
println("computed!")
"Hello"
}
fun main(args: Array<String>) {
println(lazyValue)
println(lazyValue)
}
@@ -0,0 +1,13 @@
class User(val map: Map<String, Any?>) {
val name: String by map
val age: Int by map
}
fun main(args: Array<String>) {
val user = User(mapOf(
"name" to "John Doe",
"age" to 25
))
println(user.name) // Prints "John Doe"
println(user.age) // Prints 25
}
@@ -0,0 +1,14 @@
import kotlin.properties.Delegates
class User {
var name: String by Delegates.observable("<no name>") {
prop, old, new ->
println("$old -> $new")
}
}
fun main(args: Array<String>) {
val user = User()
user.name = "first"
user.name = "second"
}