5 ms·
You didn't answer a few of the interspersed questions, so I'll press you on the records one. What about Java records are different than Kotlin's data classes b
by ragnese 6y ago
You didn't answer a few of the interspersed questions, so I'll press you on the records one.
What about Java records are different than Kotlin's data classes besides forgoing the auto-generated `copy()` method? I understand there are some implementation details, such as inheriting from a Record base class, and how it handles serialization. But I mean as a user.
Can records implement interfaces?
Can records be variants in a sealed class hierarchy?
Can records have a private primary constructor?
Can I customize the getters (to e.g., make defensive copies)?
For other people reading, the answers for Kotlin data classes are: Yes, Yes, Not Really, No.
As an aside, I understand you are an Oracle employee. When you post under this username are you acting in any kind of official capacity for Oracle? Like, is part of your job, so to speak, to have a social media presence? I'm not suggesting anything negative- it's totally fair to invest in outreach, to answer questions, clarify things, etc. I was just curious if I'm talking to "a guy who loves the project he works on" or "a representative of a company".
- pron 6y ago> What about Java records are different than Kotlin's data classes Java records, like enums (another feature that is philosophically very similar to records) aim not to reduce the boilerplate of certain operations, but to designate a subset of classes with particular semantic properties (and make those easy to express). In the case of enums, that subset is classes with a well-known, fixed set of instances; for records that is nominal tuples, i.e. immutable, unencapsulated data aggregates, similar to ML's product types. So users, the compilers, and libraries can make certain assumptions about records. For example, their semantic properties (that they are no more than a product of their component types) allow a much better serialization story for them and, indeed, record classes are serialized differently from non-record classes: Instead of invoking a no-arg constructor, their canonical constructor is invoked on deserialization. Their immutability also makes automatic implementations of equality, deconstruction and pattern matching clear and correct. Just like Kotlin couldn't do user-mode threads efficiently because they have no control over the platform, there was also little point in doing records, because that language's goal was to make it easier, syntactically, to work with the existing Java ecosystem, and, prior to records, Java programmers worked with JavaBean-like classes, so Kotlin tried to make those operations more syntactically pleasant. Java's designers, however, can have an impact on what that ecosystem does and can change its direction. > As an aside, I understand you are an Oracle employee. Yes. I work on OpenJDK. > When you post under this username are you acting in any kind of official capacity for Oracle? Absolutely not. I speak only for myself. I'm the technical lead for Project Loom, and I want to see what kind of reactions people have to it on social media (as well as conferences, customer meetings, surveys etc.). I guess I see it indirectly as part of my job, at least as far as Loom goes, as these interactions help inform how we explain the capabilities, what features people want etc.. It's nothing official, though. It's also a harmful personal addiction.
- pron 6y ago> Can records implement interfaces? Yes. > Can records be variants in a sealed class hierarchy? Yes. > Can records have a private primary constructor? The accessibility of the constructor is roughly at least that of the record class itself. If the record class is public, then the canonical constructor must be public. However, thanks to the positive answers to the two previous questions, you can have a readable-though-not-publicly-constructible record -- make it a private implementation of a sealed public interface. > Can I customize the getters (to e.g., make defensive copies)? Yes. Although in most cases, defensive copying in the constructor is sufficient.
- SureshG 6y ago> "a guy who loves the project he works on" Yes, he is Ron Pressler, project lead of Loom - https://twitter.com/pressron https://twitter.com/pressron