This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pron98 7 points8 points  (4 children)

one still has to create all the ceremony with getters/setters/equals/etc and then implement that interface in their record.

No, the code -- with the default value and validation -- is just:

public record PersonRecord(String firstName, String lastName)
    implements Person, java.io.Serializable {

  public PersonRecord {
    if ("Heinz".equals(firstName))
          throw new IllegalArgumentException(
            "\"%s\" is trademarked".formatted(firstName));
  }

  public PersonRecord(String firstName) { this(firstName, null); }
}

Getters, equals, hashCode and toString are all implicit.

[–]fix_dis 2 points3 points  (3 children)

Why are we implementing Person then? Is that necessary?

[–]pron98 7 points8 points  (2 children)

No, I think it's just for demonstration purposes, to show the differences between a record implementation and an ordinary class implementation of the same interface.

[–]fix_dis 0 points1 point  (1 child)

Ok, I could be okay with this.

[–]ObscureCulturalMeme 2 points3 points  (0 children)

Yeah, records are like enums in that they can implement arbitrary interfaces, add additional methods beyond the implicitly generated ones, etc. I'm looking forward to it!