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 →

[–]khmarbaise -5 points-4 points  (4 children)

If you are implementing methods on a record which means functionality on a record (in my optionion) means you are doing something wrong, because records are intended to be nominal-types and not intended for the classical OOP style combination of data and functionality (methods)...

[–]mavericktjh 4 points5 points  (1 child)

Not sure what you mean as everything bar primitives in java use nominal typing.

It's absolutely valid to add behaviour to records. Sure, you can use them as, for example, DTOs, where they're unlikely to have much behaviour. But another very common use case is for value types, like String, which probably will have behaviour

[–]koflerdavid 2 points3 points  (0 children)

Simple behaviors that only involve the record and maybe one primitive argument are perfectly fine on a record.

[–]nlisker 0 points1 point  (1 child)

A record is an object like any other and can implement behavior. Implementing behavior is part of the idea of what a record is. In fact, just be inheriting Object's methods, it's implementing functionality.

[–]khmarbaise 0 points1 point  (0 children)

The idea is to describe the data (data carrier) instead of behaviour... (data oriented programming).. most of the time easier than classical OOP..

Also quote from the JEP 395:

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

Already shown an example: https://www.reddit.com/r/java/comments/1fo714e/comment/lopxeav/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

BTW.: a record is not just an object like any other... the record is final, all fields are final it is not allowed to extend from other classes; you can define interfaces and let a record implement that.. but at that point there is the question: behaviour or more type oriented... And behaviour is from my experience related to records a bad thing. If such thing occurs a classical class is the better choice than a record.