data-class

Why does Kotlin data class objects have backticks?

╄→гoц情女王★ 提交于 2021-01-27 07:57:31
问题 This is my data class created using a Kotlin data class creator Plugin. data class ResponseHealthInisghts( val `data`: List<Data>, val message: String, val statusCode: Int ) This code gets work even if I remove the backticks, I wonder if it's for Java interoperability. But this variable is not a keyword but also it has backticks. why? Based on Why does this Kotlin method have enclosing backticks? this question is is a keyword for both Java and Kotlin but data is not. 回答1: You can use

Kotlin data class implementing Java interface

痴心易碎 提交于 2020-06-10 07:22:53
问题 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 description: String? = null, val imgLink: String? = null, val created: Date? = null, val accessKey: String? = null, val views: Long? = null, val comments: Long? = null, val videoLink: String? = null): Entity Which implements Java interface: public

Kotlin data class implementing Java interface

别说谁变了你拦得住时间么 提交于 2020-06-10 07:22:28
问题 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 description: String? = null, val imgLink: String? = null, val created: Date? = null, val accessKey: String? = null, val views: Long? = null, val comments: Long? = null, val videoLink: String? = null): Entity Which implements Java interface: public

dataclass copy with field ArrayList - change the ArrayList of the copied class changes the original

假装没事ソ 提交于 2020-05-31 07:02:49
问题 I have a data class like this data class Person(val id: Long = BaseDataContract.BaseData.UNDEFINED_ID.toLong(), ..... val personConsents: ArrayList<PersonConsent> = ArrayList<PersonConsent>()) I have two copies of the object: person = originalPerson.copy() Then I change the elements of personConsents for the object person - I add/delete/edit them. But by some reason I see that the same changes are happening in originalPerson object which I don't want to be. originalPerson is not supposed to

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

被刻印的时光 ゝ 提交于 2020-01-11 08:19:08
问题 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? 回答1: Shortly before

extension function for Kotlin data class

我的未来我决定 提交于 2020-01-06 07:20:11
问题 I have a data class which looks something like this data class SuggestionResponse( val metadata: Metadata, val response: Response ) data class Response( ///blah blah ) data class Metadata( val timeleft: String, val totalTime: String ) Now my requirement to transform this data into a different type of data object.I want to write an extension function to do this task. let the name of function be hello I would like to call this extension function like this suggestionResponse.hello() how do I

Reference outside the sealed class in Kotlin?

不问归期 提交于 2019-12-31 02:45:15
问题 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

How to set values to data class if some size is specified in kotlin?

懵懂的女人 提交于 2019-12-24 19:42:58
问题 I have a data class in kotlin like this: data class myDataClass(val myArr: ArrayList<Char>) Now, suppose I create an instance of it as follows: val myData = myDataClass(x) // x is an integer; 1 <= x <= 9 I want that myData should have the following data: println(myData.myArr) // [A, B, C, D, ...] 回答1: It's possible: data class myDataClass(val myArr: ArrayList<Char>) { constructor(i: Int) : this(ArrayList((0..i).map { ('A' + it).toChar() })) } But the truth is, it's a pretty strange code 来源:

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

别等时光非礼了梦想. 提交于 2019-12-19 05:53:42
问题 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 adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File." SoundController - upload function /** * @Security("is_granted('IS_AUTHENTICATED_FULLY')") * @Route("/song/upload", name="upload_song")

Kotlin data class copy method not deep copying all members

三世轮回 提交于 2019-12-18 19:07:18
问题 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 = Foo(5, bar, mutableListOf(1, 2, 3)) println("foo : $foo") val barCopy = bar.copy() val fooCopy = foo.copy() foo.a = 10 bar.x = 2 foo.list.add(4) println("foo : $foo") println("fooCopy: $fooCopy") println("barCopy: $barCopy") } data class Foo(var a: Int, val bar