Kotlin Data Class Constructor Problem
I’ve been trying to do something with a model class in Kotlin that I’ve done many times before in Java, and I’ve been left without a clean way to do what I need.
Here’s what I’m trying to do in java:
The key things to note are the calls to name.trim()
, and emailId.toLowerCase()
in the constructor. These calls ensure that every User
object created has a “valid” value for name
and emailId
.
Initially, in Kotlin, this is what I had:
The problem here is that I don’t see any way of putting in the calls to trim()
and toLowerCase()
.
Do you know of any ways to solve this problem? Please let me know: @gopalkri!
Originally published at gopalkri.com on August 15, 2017.