Questions tagged [data-class]

Refers to data classes feature in Kotlin programming language.

380 questions
19
votes
2 answers

How to let a data class implements Interface / extends Superclass properties in Kotlin?

I have several data classes which include a var id: Int? field. I want to express this in an interface or superclass, and have data classes extending that and setting this id when they are constructed. However, if I try this: interface B { var id:…
Edy Bourne
  • 5,679
  • 13
  • 53
  • 101
19
votes
2 answers

Is there a way to identify a Kotlin data class from a regular Kotlin class?

Is there a way to identify a Kotlin data class from a regular Kotlin class? Like using reflection maybe?
Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
18
votes
1 answer

How do I make a python dataclass inherit __hash__?

The following will work, but I'd rather not need to repeat the __hash__ in each subclass. Is there a way to tell the dataclass to inherit the hash function (i.e. not set it to None)? from dataclasses import dataclass @dataclass class Hashable: …
Brian Bruggeman
  • 5,008
  • 2
  • 36
  • 55
17
votes
1 answer

How to update data classes implementing a common interface

I'm struggling with data classes and polymorphism. I want to benefit from the immutability, but still be able to update my state. For this, I'm hoping to be able to use the copy function. Let's give an example. I have this class hierarchy: interface…
Stim
  • 1,455
  • 14
  • 28
15
votes
2 answers

How should I add a field containing a list of dictionaries in Marshmallow Python?

In Marshmallow in order to have a list field you can use: include_in = fields.List(cls_or_instance=fields.Str(), default=['sample1', 'sample2']) This is OK, but I have a new requirement to have a list of dictionaries in a…
Alireza
  • 6,497
  • 13
  • 59
  • 132
15
votes
2 answers

Access function before calling superclass constructor in Kotlin data class

I'm using data classes in Kotlin to significantly reduce the amount of Java code I would otherwise have to write. However, in one of my Java classes, I'm not sure what to do to achieve the same result in Kotlin. My Java class looks a bit like…
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
14
votes
2 answers

Is there any way to transform the value of a property at data class construction time?

When creating a data class I frequently find that I want to transform one of the properties, usually to normalize it or to make a defensive copy. For example, here I want productCode to always be lowercase: data class Product(val productCode:…
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
14
votes
2 answers

The form's view data is expected to be an instance of class ... but is a(n) string

I am currently receiving the following error: "The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by…
P.Belberov
  • 503
  • 1
  • 5
  • 18
13
votes
1 answer

Kotlin: sealed class cannot "contain" data classes? Why?

OK, now that Kotlin is officially out and I am starting to play with it again, I am quite confused that I need to choose between the advantages of sealed and data but somehow can't have both. This, for example, seems to make sense to me, but does…
david.mihola
  • 12,062
  • 8
  • 49
  • 73
12
votes
1 answer

Is it possible to nest dataclass in kotlin?

I'm trying to achieve similar data class definition like the following C one: struct A { int b; struct { int d; } c; }; According to Dmitry Jemerov it is possible, but he didn't provide any code…
majkrzak
  • 1,332
  • 3
  • 14
  • 30
12
votes
3 answers

Deserialize a Firebase Data-Snapshot to a Kotlin data class

Hi I have a Kotlin data class as follows data class User ( @get:Exclude val gUser: Boolean, @get:Exclude val uid: String, @get:PropertyName("display_name") val displayName: String, @get:PropertyName("email") val…
aidan8181
  • 175
  • 1
  • 3
  • 7
11
votes
1 answer

How to test data class on Kotlin?

I have data class'es in my Kotlin project, what I use for JSON Rest responses.. Example: data class WeatherResponse(val city: String, val temperature: Double, val humidity: Double) To fullfill code coverage constraints, I would like to write some…
Noam Silverstein
  • 819
  • 2
  • 12
  • 18
11
votes
3 answers

How to ignore a filed of Entity with Room

I have a data class that I created an Entity from that for my database. this is my data class: @Entity @Parcelize data class Tapligh( @PrimaryKey(autoGenerate = true) var id: Long, @SerializedName("title") var title: String?, …
Ehsan
  • 2,676
  • 6
  • 29
  • 56
11
votes
3 answers

How to deserialize inherited Kotlin data classes with Gson

In an Android App I need to deserialize Json data for Kotlin data classes, with a single abstraction level. But I don't have any idea, to place correct properties in constructors. As a simple version, let's say I have a Shape: abstract class…
elcolto
  • 961
  • 7
  • 11
11
votes
2 answers

Kotlin data classes and nullable types

I'm new to Kotlin and I don't know why compiler complains about this code: data class Test(var data : String = "data") fun test(){ var test: Test? = Test("") var size = test?.data.length } Compiler complains with test?.data.length, it says…
1
2
3
25 26