data-class

Kotlin data class copy method not deep copying all members

旧时模样 提交于 2019-11-30 17:57:15
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: Bar, val list: MutableList<Int> = mutableListOf()) data class Bar(var x: Int = 0) Output: foo : Foo(a

Equals method for data class in kotlin

自作多情 提交于 2019-11-30 12:25:30
问题 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 objects. val board1 = PuzzleBoard(intArrayOf(1,2,3,4,5,6,7,8,0)) val board2 = PuzzleBoard(intArrayOf(1,2,3,4,5,6,7,8,0)) But still the following statements return false. board1 == board2 board1.equals(board2) 回答1: In Kotlin data classes equality check,

Equals method for data class in kotlin

流过昼夜 提交于 2019-11-30 04:40:56
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 objects. val board1 = PuzzleBoard(intArrayOf(1,2,3,4,5,6,7,8,0)) val board2 = PuzzleBoard(intArrayOf(1,2,3,4,5,6,7,8,0)) But still the following statements return false. board1 == board2 board1.equals(board2) In Kotlin data classes equality check, arrays, just like other classes, are compared using equals(...) , which compares the arrays references, not

kotlin data class + bean validation jsr 303

谁都会走 提交于 2019-11-29 21:20:40
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 id: Long? = null, @Size(min=5, max=15) val name: String ) The @Size annotation has no effect here, making me able to save a user with a name of 1 character. It works well when executing the very same example but in a Java class instead of Kotlin. This makes me think of a Kotlin problem. Thanks in advance for you help ! You need to use Annotation use-site targets

How to extend a dataclass with toString

 ̄綄美尐妖づ 提交于 2019-11-29 09:04:17
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? In Kotlin, extension functions cannot override member functions, moreover, they are resolved statically . It implies

Deserialize a Firebase Data-Snapshot to a Kotlin data class

蹲街弑〆低调 提交于 2019-11-29 05:02:10
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 email: String, @get:PropertyName("account_picture_url") val accountPicUrl: String, @get:PropertyName("provider") val provider: String ) I am able to serialize the object without an issues. But i'm having trouble deserializing the object when doing a firebase query. Currently this is what i'm doing to get the data _firebaseReference.child(getString(R.string.firebase_users_key)).child

Kotlin data class of RealmObject

房东的猫 提交于 2019-11-29 04:56:28
问题 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 db, so I use Realm to save it. But as we know, if I want to save the class to Realm, the AuthToken class has to extend RealmObject . That's the problem, Kotlin says data classes can't extend classes. so I give up data class, just using a normal Kotlin class as a model then another question comes: Kotlin class

kotlin data class + bean validation jsr 303

↘锁芯ラ 提交于 2019-11-28 17:26:39
问题 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 id: Long? = null, @Size(min=5, max=15) val name: String ) The @Size annotation has no effect here, making me able to save a user with a name of 1 character. It works well when executing the very same example but in a Java class instead of Kotlin. This makes me

Kotlin Data Class from Json using GSON

最后都变了- 提交于 2019-11-28 16:36:45
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 json key to any variables of the kotlin data class like the @SerializedName annotation in java variables ? Anton Holovin Data class: data class Topic( @SerializedName("id") val id: Long, @SerializedName("name") val name: String, @SerializedName("image") val image: String, @SerializedName("description") val description: String ) to JSON: val gson = Gson() val json = gson

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

时光怂恿深爱的人放手 提交于 2019-11-28 08:35:41
How can I read the value of property in a Kotlin data class instance if the property name is only known at runtime? Here is a function to read a property from an instance of a class given the property name ( throws exception if property not found, but you can change that behaviour ): import kotlin.reflect.KProperty1 import kotlin.reflect.full.memberProperties @Suppress("UNCHECKED_CAST") fun <R> readInstanceProperty(instance: Any, propertyName: String): R { val property = instance::class.memberProperties // don't cast here to <Any, R>, it would succeed silently .first { it.name == propertyName