I could not find a better heading to describe how I can avoid code duplication (expression required) in this Kotlin class:
class Person(email: String) {
var email: String = email
set(value) {
require(value.trim().isNotEmpty(), { "The email cannot be blank" })
field = value
}
init {
require(email.trim().isNotEmpty(), { "The email cannot be blank" })
}
}
In java, I will have a setter with name verification, and then I would call it from the constructor.
What is the idiomatic way to do this in Kotlin?
source
share