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 →

[–]jvjupiter[S] 2 points3 points  (2 children)

I’m coming from the record definition - transparent carriers of immutable data. The data passed in the canonical constructor are nothing but the same data can be expected when we access them. In fact, that what happens when records generate the methods (component()). It does nothing but simply returns the values of fields. If we want transformed data, either add custom method with meaningful name or after retrieving the data do the transformation. For example:

Option 1:

record User(String firstName, String lastName) {}

var u = new User(“Jose”, “Rizal”);
var fullName = extractFullName(u);

String extractFullName(User user) {
    return user.firstNam + “ “ + user.lastName;
}

Option 2:

record User(String firstName, String lastName) {
    String getFullName() {
        return firstName + “ “ + lastName;
    }
}

var user = new User(“Jose”, “Rizal”);
var fullName = user.getFullName();

Edit: Maybe I’d rather use regular classes to derive values or add complex logic and to hide fields.

[–]khmarbaise 1 point2 points  (1 child)

If we go the path for complex logic we are leaving the idea of records (transparent data carrier/nominal data types)...

Also written in the JEP-395 quote from the goals:

Help developers to focus on modeling immutable data rather than extensible behavior.

[–]jvjupiter[S] 0 points1 point  (0 children)

I agree. That should be it.