ecampolo
11/21/2017, 10:54 AMclass 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" })
}
}
I tried with Delegation but feels a little bit complex since I would need to have NotEmptyString , NotEmptyCollection, or any kind of validation. Also I want to avoid those extra objects per property.Andreas Sinz
11/21/2017, 12:59 PMclass Person(_email: String) {
var email: String = email
set(value) {
require(value.trim().isNotEmpty(), { "The email cannot be blank" })
field = value
}
init {
email = _email
}
}
Just assign the value of the constructor-argument to your email
-property?ecampolo
11/21/2017, 1:04 PMclass Person(email: String) {
var email: String = email
set(value) {
require(value.trim().isNotEmpty(), { "The email cannot be blank" })
field = value
}
init {
this.email = email
}
}
Andreas Sinz
11/21/2017, 1:05 PMecampolo
11/21/2017, 1:07 PMecampolo
11/21/2017, 1:08 PMecampolo
11/21/2017, 1:09 PMecampolo
11/21/2017, 1:10 PMPerson(email = "")
, the object is first initialized with an empty value and then executing the init block calling the setter, resulting in an exceptionAndreas Sinz
11/21/2017, 1:11 PMecampolo
11/21/2017, 1:11 PMAndreas Sinz
11/21/2017, 1:12 PMAndreas Sinz
11/21/2017, 1:15 PMecampolo
11/21/2017, 1:20 PMecampolo
11/21/2017, 1:20 PMecampolo
11/21/2017, 1:21 PM