umar
04/21/2017, 1:12 PMimport kotlin.reflect.KProperty
class StringValidator {
private lateinit var field: String
operator fun getValue(thisRef: Any?, property: KProperty<*>) = field
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
if(value.isEmpty()) throw IllegalArgumentException("Empty ${property.name}")
field = value
}
}
And use in business code:
class Person(name: String) {
var name by StringValidator()
init {
this.name = name
}
}