Kotlin Data Class Constructor Problem

Gopal Sharma
Surya Dev Blog
Published in
1 min readAug 15, 2017

--

Source: https://kotlinlang.org/docs/reference/faq.html#where-can-i-get-an-hd-kotlin-logo

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.

--

--

I do web and iOS stuff for a living. Mega Michigan Football, Bengaluru FC and Arsenal FC fan. Mediocre photographer.