Last active
May 15, 2017 09:37
-
-
Save KucherenkoIhor/5c5b2eadd2379c8b1c8de345e7db848f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StringProvider { | |
operator fun provideDelegate( | |
thisRef: Main, | |
prop: KProperty<*> | |
): ReadOnlyProperty<Main, String> { | |
if(checkProperty(thisRef, prop.name)) { | |
return object : ReadOnlyProperty<Main, String> { | |
override fun getValue(thisRef: Main, property: KProperty<*>): String { | |
return "true string" | |
} | |
} | |
} else { | |
return object : ReadOnlyProperty<Main, String> { | |
override fun getValue(thisRef: Main, property: KProperty<*>): String { | |
return "false string" | |
} | |
} | |
} | |
} | |
private fun checkProperty(thisRef: Main, name: String) = name.length > 4 | |
} | |
fun bindString() = StringProvider() | |
class Main { | |
val fake by bindString() | |
val valid by bindString() | |
} | |
fun main(args: Array<String>) { | |
Main().apply { | |
println(fake) //false string | |
println(valid) //true string | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment