Questions tagged [data-class]

Refers to data classes feature in Kotlin programming language.

380 questions
349
votes
14 answers

Extend data class in Kotlin

Data classes seem to be the replacement to the old-fashioned POJOs in Java. It is quite expectable that these classes would allow for inheritance, but I can see no convenient way to extend a data class. What I need is something like this: open data…
Dmitry
  • 4,232
  • 2
  • 15
  • 13
148
votes
3 answers

Kotlin Data Class from Json using GSON

I have Java POJO class like this: class Topic { @SerializedName("id") long id; @SerializedName("name") String name; } and I have a Kotlin data class Like this data class Topic(val id: Long, val name: String) How to provide the…
erluxman
  • 18,155
  • 20
  • 92
  • 126
80
votes
5 answers

Equals method for data class in Kotlin

I have the following data class data class PuzzleBoard(val board: IntArray) { val dimension by lazy { Math.sqrt(board.size.toDouble()).toInt() } } I read that data classes in Kotlin get equals()/hashcode() method for free. I instantiated two…
Vaibhav
  • 971
  • 1
  • 7
  • 8
78
votes
2 answers

How to extend a data class with toString

I have created a data class data class Something ( val a : String, val b : Object, val c : String ) as later in my program, I need the string representation of this data class I tried to extend the toString method. override fun…
user4233758
76
votes
12 answers

Kotlin data class copy method not deep copying all members

Could someone explain how exactly the copy method for Kotlin data classes work? It seems like for some members, a (deep) copy is not actually created and the references are still to the original. fun test() { val bar = Bar(0) val foo =…
triad
  • 20,407
  • 13
  • 45
  • 50
72
votes
2 answers

kotlin data class + bean validation jsr 303

I'm trying to get Kotlin working with jsr 303 validation on a spring-data-rest project. Given the following data class declarartion : @Entity data class User( @Id @GeneratedValue(strategy = javax.persistence.GenerationType.AUTO) var…
pellenberger
  • 813
  • 1
  • 6
  • 7
63
votes
1 answer

Extending data class from a sealed class in Kotlin

I have a set of data classes that share some common fields, So ideally I'd like to declare those in a supertype (Message in this example), and be able to write functions that operate on the supertype if they need access to these common fields…
f2prateek
  • 2,034
  • 2
  • 20
  • 26
47
votes
10 answers

Property include/exclude on Kotlin data classes

Suppose I only want one or two fields to be included in the generated equals and hashCode implementations (or perhaps exclude one or more fields). For a simple class, e.g.: data class Person(val id: String, val name: String) Groovy has…
Jonathan Schneider
  • 26,852
  • 13
  • 75
  • 99
46
votes
4 answers

Kotlin data class: how to read the value of property if I don't know its name at compile time?

How can I read the value of property in a Kotlin data class instance if the property name is only known at runtime?
Elifarley
  • 1,310
  • 3
  • 16
  • 23
41
votes
4 answers

Proguard - do not obfuscate Kotlin data classes

In my project I use AutoValue for my old model classes. I started using Kotlin and I want to use Data Classes instead of AutoValue. I want to disable the obfuscation for all Data classes in my Data layer but to keep obfuscating the other classes in…
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
36
votes
3 answers

only classes are allowed on the left hand side of a class literal

I know a lot of similar questions here in StackOverflow, but nothing solved mine. I have a generic data class: data class ServiceCall(val result: T?, val exception: String?, val pagination: String?, val stringResult: String?) I am trying…
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
36
votes
2 answers

Kotlin data class implementing Java interface

I'm trying to introduce Kotlin into my current project. I've decided to begin with entities, which seem to map perfectly to data classes. For example I have a data class: data class Video(val id: Long, val ownerId: Long, val title: String, val…
Odysseus
  • 1,032
  • 2
  • 12
  • 14
36
votes
2 answers

Kotlin data class of RealmObject

I'm using Kotlin and Realm to write a data class data class AuthToken(val register: Boolean, val token: String, val tokenSecret: String, val user: AuthUser) I have to save the data to…
Allen Vork
  • 1,536
  • 3
  • 16
  • 29
30
votes
4 answers

Is there a way to require a generic type to be a data class in Kotlin?

The following does not work, but hopefully helps you understand what I mean: class Example In case you would like to know what I am trying to accomplish, this is an example of what I had in mind: class Repository where T : Entity,…
user6427178
24
votes
2 answers

Is Kotlin data class serializable by default?

After knowing Kotlin, love the data class. I could replace Java classes that has equal and hash and toString to it. Most of these Java classes are serializable class. So my question is, when we convert to data class, do I still need to make it…
Elye
  • 53,639
  • 54
  • 212
  • 474
1
2 3
25 26