Skip to content

Instantly share code, notes, and snippets.

@KucherenkoIhor
Last active May 15, 2017 09:37
Show Gist options
  • Save KucherenkoIhor/5c5b2eadd2379c8b1c8de345e7db848f to your computer and use it in GitHub Desktop.
Save KucherenkoIhor/5c5b2eadd2379c8b1c8de345e7db848f to your computer and use it in GitHub Desktop.
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