Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin's data class == C#'s struct?

Tags:

c#

kotlin

I used C# before and there we can define a struct which will be a value type. I'm learning Kotlin now and as far as I know kotlin data-class compares by value, can copy by value etc. Found some discussions online about this but it wasn't straightforward and I'd like someone skilled in C# and Kotlin to clear this up: can I interpret Kotlin's data class like a C# struct and call it a day? If no, what are the differences?

like image 426
agiro Avatar asked Dec 27 '17 15:12

agiro


People also ask

How do I call a Kotlin data class?

data class User(val name: String, val age: Int) fun main(args: Array<String>) { val u1 = User("John", 29) val u2 = u1. copy() val u3 = u1. copy(name = "Amanda") println("u1 hashcode = ${u1. hashCode()}") println("u2 hashcode = ${u2.

When should I use Kotlin data class?

Answer: Kotlin provides a special type of class called data class, which is usually used for objects that act as a store for data properties and has no business logic or member functions. It provides a lot of advantages with reduced boilerplate code.

What is meant by data class in Kotlin?

Data classes specialize in holding data. The Kotlin compiler automatically generates the following functionality for them: A correct, complete, and readable toString() method. Value equality-based equals() and hashCode() methods.

Which is the correct syntax of data class in Kotlin?

In Kotlin, this type of class is known as data class and is marked as data. The compiler automatically derives the following functions : equals() hashCode()

What are the requirements for a data class in Kotlin?

The primary constructor needs to have at least one parameter. All primary constructor parameters need to be marked as val or var. Data classes cannot be abstract, open, sealed, or inner. The class may extend other classes or implement interfaces. If you are using Kotlin version before 1.1, the class can only implement interfaces.

What are the parameters of the primary constructor in Kotlin?

The parameters of the primary constructor must be marked as either val (read-only) or var (read-write). The class cannot be open, abstract, inner or sealed. The class may extend other classes or implement interfaces. If you are using Kotlin version before 1.1, the class can only implement interfaces.

How to implement interfaces in Kotlin?

If you are using Kotlin version before 1.1, the class can only implement interfaces. When you run the program, the output will be: When you declare a data class, the compiler automatically generates several functions such as toString (), equals (), hashcode () etc behind the scenes. This helps to keep you code concise.

What does Kotlin mean?

Answer: Kotlin provides a special type of class called data class, which is usually used for objects that act as a store for data properties and has no business logic or member functions. It provides a lot of advantages with reduced boilerplate code.


1 Answers

The main difference between Kotlin data classes and C# structs is that the Kotlin data classes are still classes, they are passed by reference (a referential type, speaking in terms of C#) and stored in the same heap with other objects (not taking possible JVM optimizations into account) instead of the stack, in the same form as the other objects.

The copy and equality check implementations for data classes are just generated into the methods of the class and are called as instance methods in a JVM-natural way.

Some limitations that the data classes have in common with structs are caused by a different reason: for example, data classes are final because of unclear semantics of the auto-generated functions that would come from data class inheritance.

like image 85
hotkey Avatar answered Oct 08 '22 12:10

hotkey