What is the purpose of "data class"?

Asked

Viewed 1,794 times

6

In Kotlin it is possible to create a class as follows:

data class Dragon(private val name: String, val glass: Int)

What is the main objective of the data class in Kotlin? When should we use?

  • Who denied the question could at least explain the reason why I can edit it and try to collaborate more with our community.

1 answer

7


Also called registration, is to be a simple structure basically with data, without behaviors, other than those necessary for the basic infrastructure of the language, as a comparison (equals()), hash (hashCode()), access methods for defined fields (compatible with Java Beans), copy (copy()) and stringification (toString()), beyond the builder, of course. So we usually say that she is POD (Plain Old Data).

Cannot inherit from data classes. Syntax is greatly simplified:

data class User(val name: String, val age: Int)

It would be more or less the same as doing in Java:

public class User {
    private String name;
    private int age;

    public User() {
        this.name = "";
        this.age = 0;
    }

    public User(String name) {
        this.name = name;
        this.age = 0;
    }

    public User(int age) {
        this.name = "";
        this.age = age;
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String component1() { //para acesso posicional e deconstrução
        return name;
    }

    public int component2() {
        return age;
    }

    public User copy() {
        return new User(name, age);
    }

    public User copy(String newName) {
        return new User(newName, age);
    }

    public User copy(int newAge) {
        return new User(name, newAge);
    }

    public User copy(String newName, int newAge) {
        return new User(newName, newAge);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (age != user.age) return false;
        return name != null ? name.equals(user.name) : user.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

I put in the Github for future reference.

Isn’t it wonderful? This is foreseen in C#, it’s just not known when, it was supposed to be since version 6. In C# it’s even more complex.

It is possible to have extra behaviors in the class and can extend (not in the sense of inheritance) as a normal class.

Documentation.

See more in What is the difference between Kotlin data class and Scala case class?.

  • If declared as var would have the setter right?!

  • Yes, the class date may be immutable, but with the use of var instead of val he may be changeable.

  • @acklay yes, I was now seeing more details and it is possible in this case.

  • 1

    A reason I found for data class were the final methods equals()and toString() are "wrong". However, by allowing them to be extended the same happens.

  • @ramaral define "wrong"

  • With this question you forced me to think better and in fact in case of being extended the methods are not "wrong" taking into account that they only consider the properties resulting from the variables declared in the constructor. In the case of inheritances, oh yes, if the daughter class declared more variables in its constructor, they would not be considered in those methods.

  • @ramaral but in data class has no inheritance.

  • Yes, perhaps one of the reasons (I don’t know if there will be others) for not allowing inheritance is to ensure that the automatically created methods are not "wrong" (use all variables declared in the constructor). I was wrong when I said this would happen in case the class was extended.

  • Sounds like structs.

  • @felipsmartins But it’s a real class :)

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.