# Suggestion for parameterless data class

**URL:** <https://discuss.kotlinlang.org/t/suggestion-for-parameterless-data-class/1990>\
**Category:** Language Design\
**Created:** [September 28, 2016, 12:13pm UTC](https://discuss.kotlinlang.org/t/suggestion-for-parameterless-data-class/1990 "2016-09-28T12:13:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![funbro](https://avatars.discourse-cdn.com/v4/letter/f/3bc359/32.png) [@funbro](https://discuss.kotlinlang.org/u/funbro)\
**Post date:** [September 28, 2016, 12:13pm UTC](https://discuss.kotlinlang.org/t/suggestion-for-parameterless-data-class/1990/1 "2016-09-28T12:13:00Z")

</div>

I was trying [spring’s form input validation](https://spring.io/guides/gs/validating-form-input/) with kotlin. If I created `PersonForm` using `data class` like this:

```
data class PersonForm (
    @Size(min=2, max=30)
    @NotBlank
    var name: String = "",
    //...
) {
    constructor(): this("") { }
}

```

the validation doesn’t work, the form was just passed successfully. Adding secondary empty constructor doesn’t work too.

The only solution is to use plain class:

```
class PersonForm {
    @Size(min=2, max=30)
    @NotBlank
    var name: String = ""
    //...
}

```

but I would lose the benefits of data class.

It seems the validator expects the form class must have primary empty constructor.

So I would like to suggest if we can have parameterless data class? e.g.

```
data class PersonForm {
    @Size(min=2, max=30)
    @NotBlank
    var name: String = ""
    //...
}

```

If I’m not wrong, data class is almost similar to case class in scala but scala case class can have empty parameter.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [September 28, 2016, 12:21pm UTC](https://discuss.kotlinlang.org/t/suggestion-for-parameterless-data-class/1990/2 "2016-09-28T12:21:28Z")

</div>

A data class uses properties defined in the primary constructor to generate its `equals`, `hashCode`, `toString` and other methods. A data class declared in the way that you suggest would have meaningless implementations of those methods, which is very unlikely to be what you actually want.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [September 28, 2016, 5:56pm UTC](https://discuss.kotlinlang.org/t/suggestion-for-parameterless-data-class/1990/3 "2016-09-28T17:56:15Z")

</div>

> [@funbro](#):
>
> It seems the validator expects the form class must have primary empty constructor.

It’s unlikely that the validator could know which of constructors is primary.

Try to inspect if there’s a difference in the generated bytecode in how the `name` property is annotated in case of data class and plain class.

---

<div class="post-metadata">

**Author:** ![funbro](https://avatars.discourse-cdn.com/v4/letter/f/3bc359/32.png) [@funbro](https://discuss.kotlinlang.org/u/funbro)\
**Post date:** [September 28, 2016, 6:58pm UTC](https://discuss.kotlinlang.org/t/suggestion-for-parameterless-data-class/1990/4 "2016-09-28T18:58:01Z")

</div>

Alright, I checked them with JD-GUI, here what I found out for the generated `name` field:

plain class:

```
public final class PersonForm
{
    @Size(min=2, max=30)
    @NotBlank
    @NotNull
    private String name = "";

    ...
}

```

data class:

```
public final class PersonForm
{
    @NotNull
    private String name;

    public PersonForm(@NotNull @Size(min=2, max=30) @NotBlank String name, ...)
    {
      this.name = name;
      ...
    }

    ...
}

```

So on the data class, the @Size and @NotBlank annotation aren’t put correctly on the `private String name` field declaration, instead they are put on the constructor’s parameters.

Not related with the above issue, just curious that there are these methods generated for each constructor fields on data class:

```
  @NotNull
  public final String component1()
  {
    return this.name;
  }

  @NotNull
  public final Integer component2()
  {
    return this.age;
  }

```

What are they used for?

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [September 29, 2016, 11:09am UTC](https://discuss.kotlinlang.org/t/suggestion-for-parameterless-data-class/1990/5 "2016-09-29T11:09:40Z")

</div>

You can use `@field:Name` syntax to ensure that the annotation is applied to the field and not the constructor parameter: [http://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets](http://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets)

componentN() functions are used for destructuring declarations: [http://kotlinlang.org/docs/reference/multi-declarations.html](http://kotlinlang.org/docs/reference/multi-declarations.html)

---

<div class="post-metadata">

**Author:** ![funbro](https://avatars.discourse-cdn.com/v4/letter/f/3bc359/32.png) [@funbro](https://discuss.kotlinlang.org/u/funbro)\
**Post date:** [September 29, 2016, 1:51pm UTC](https://discuss.kotlinlang.org/t/suggestion-for-parameterless-data-class/1990/6 "2016-09-29T13:51:37Z")

</div>

> [@yole](#):
>
> You can use @field:Name syntax to ensure that the annotation is applied to the field and not the constructor parameter: [http://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets](http://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets)

I’ve tried adding `@field:Name` and then `@field:name`, but they gave compile error. I think you meant `@field:<annotation_name>`, thus:

```
data class PersonForm (
    @field:Size(min=2, max=30)
    @field:NotBlank
    var name: String = "",

```

It took me awhile to figure out that. The documentation is a bit confusing too, it mentioned `@field:Ann` but don’t describe what is `Ann` at that section. It is mentioned at above which requires some reading. I was thinking I have to add new annotation `@field:??`, but turns out it’s just prefixing @field to existing annotation.

**Suggestion:**  
I think it’s better if kotlin compiler put the annotation into field location by default if the constructor param is `val` or `var` in order to avoid the overhead of `@field` prefix, as most people would expect they are annotated on field location.

---

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [September 29, 2016, 2:50pm UTC](https://discuss.kotlinlang.org/t/suggestion-for-parameterless-data-class/1990/7 "2016-09-29T14:50:37Z")

</div>

You could improve the documentation, it’s on GitHub. That would be great!
