윤동환
04/06/2023, 7:03 AMdata class Member(
val firstName: String,
val lastName: String,
val phone: String,
)
What i want to do is force Member instance to created with triming it’s String properties. So there are not Member instances that has blank values at String type properties(lastName, firstName)!
The easiest way is binding trimed value at Member’s primary constructor, but it is still possibile create instance that have with blank value at their String type properties.
Can i ask you the appropriate way?Loney Chou
04/06/2023, 7:19 AMLoney Chou
04/06/2023, 7:26 AMclass Data(int: Int) {
val int: Int = int.coerceAtLeast(0)
}
data class Data(val int: Int) {
companion object {
fun coerced(int: Int): Data {
return Data(int.coerceAtLeast(0))
}
}
}
Loney Chou
04/06/2023, 7:32 AMcopy
function can access the primary constructor and this will bypass your validation process in places other than init {}
block. The choice is yours, but be aware of these potential bugs.윤동환
04/06/2023, 8:38 AM