data-class

How to extend a dataclass with toString

吃可爱长大的小学妹 提交于 2019-12-18 05:27:06
问题 I have created a dataclass data class Something ( val a : String, val b : Object, val c : String ) as later in my program I need the string representation of this dataclass I tried to extend the toString method. override fun Something.toString() : String = a + b.result() + c The problem here is, it does not allow extending(overriding) the toString funtion, as it is not applicable to top level functions. How to properly override/extend the toString method of a custom dataclass? 回答1: In Kotlin,

Multiple constructors on an immutable (data) class

和自甴很熟 提交于 2019-12-12 21:14:47
问题 I'm trying to implement an immutable data class with more than one constructor. I felt that something like this should be possible: data class Color(val r: Int, val g: Int, val b: Int) { constructor(hex: String) { assert(Regex("#[a-fA-F0-6]{6}").matches(hex), { "$hex is not a hex color" } ) val r = hex.substring(1..2).toInt(16) val g = hex.substring(3..4).toInt(16) val b = hex.substring(5..6).toInt(16) this(r,g,b) } } Of course, it isn't: Kotlin expects the call to the main constructor be

Defensive copy of a mutable collection in Kotlin data class

旧时模样 提交于 2019-12-10 13:13:45
问题 I want to have a data class accepting a read-only list: data class Notebook(val notes: List<String>) { } But it can accept MutableList as well, because it is a subtype of the List . For example the following code modifies the passed in list: fun main(args: Array<String>) { val notes = arrayListOf("One", "Two") val notebook = Notebook(notes) notes.add("Three") println(notebook) // prints: Notebook(notes=[One, Two, Three]) } Is there a way how perform defensive copy of the passed in list in the

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

故事扮演 提交于 2019-12-08 14:55:56
问题 Is there a way to identify a Kotlin data class from a regular Kotlin class? Like using reflection maybe? 回答1: Since 1.1 there is an isData property on the class MyDataClass::class.isData 回答2: You can't read data annotation by reflection because it has default retention( CLASS ). You can try to use some heuristics, like check that it contains next methods: public final copy public final component{N} public static copy$default But note that somethings of this are implementation details and may

Kotlin data class copy function not working

倾然丶 夕夏残阳落幕 提交于 2019-12-04 11:21:18
Maybe I'm misinterpreting how the copy function of a data class works or maybe there's a bug, but the following is example of the copy function not working as expected: Kotlin: data class A { public var x: String? = null public var y: String? = null public var z: B = B.ONE } enum class B { ONE TWO THREE } Java A a1 = new A() a1.setX("Hello") a1.setY("World") a1.setZ(B.TWO) A a2 = a1.copy() // a2.x is null // a2.y is null // a2.z is B.ONE It seems that copy is just making a new instance of A and not copying the values. If I put the variables in the constructor, the values are assigned, but then

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

我的未来我决定 提交于 2019-12-03 15:54:28
问题 I know a lot of similar questions here in StackOverflow, but nothing solved mine. I have a generic data class: data class ServiceCall<out T>(val result: T?, val exception: String?, val pagination: String?, val stringResult: String?) I am trying to use like this: Gson().fromJson(json, ServiceCall<SurveyListModel>::class.java).result IDE shows error: only classes are allowed on the left hand side of a class literal How to solve this? Thanks in advance. 回答1: You can't use generics with class ,

Proguard - do not obfuscate Kotlin data classes

与世无争的帅哥 提交于 2019-12-03 11:17:25
问题 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 the package. Is there a way to do this? I would expect to have something like this in my Proguard file: -keepclassmembers data class example.data_layer.** { *; } 回答1: To fix the problem I moved the model classes to model package and added new

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

女生的网名这么多〃 提交于 2019-12-03 06:12:37
I know a lot of similar questions here in StackOverflow, but nothing solved mine. I have a generic data class: data class ServiceCall<out T>(val result: T?, val exception: String?, val pagination: String?, val stringResult: String?) I am trying to use like this: Gson().fromJson(json, ServiceCall<SurveyListModel>::class.java).result IDE shows error: only classes are allowed on the left hand side of a class literal How to solve this? Thanks in advance. You can't use generics with class , as easily observable here: List<Int>::class.java It gives you the same error. To use the generic typ in GSON

Reference outside the sealed class in Kotlin?

懵懂的女人 提交于 2019-12-02 01:17:35
I'm trying to create a class that uses its own state to operate on the state of an external object that it holds a reference to. The external object can be of class A or B, which are similar, but not controlled by the author. So a sealed class is created to access their common attributes, per this earlier answer from @SimY4 . // *** DOES NOT COMPILE *** class A { // foreign class whose structure is not modifiable val prop get()= "some string made the Class-A way" } class B { // foreign class whose structure is not modifiable val prop get()= "some string made the Class-B way" } data class

Kotlin: sealed class cannot “contain” data classes? Why?

穿精又带淫゛_ 提交于 2019-12-01 15:29:20
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 not compile: sealed class Expr { data class Const(val number: Double) : Expr() data class Sum(val expr1 : Expr, val expr2 : Expr) : Expr() } because the data classes cannot extend other classes. Is there something I am missing? Shortly before having entered Beta state, Kotlin team had decided to add certain limitations on data classes usage ( see this