Kotlin / KEEP

Kotlin Evolution and Enhancement Process
Apache License 2.0
3.39k stars 361 forks source link

JVM records support #233

Open ilya-g opened 3 years ago

ilya-g commented 3 years ago

This proposal is to introduce JVM records support in Kotlin.

Essentially, it covers two aspects: how records declared in Java should be seen in Kotlin and how to declare a record in Kotlin.

The proposal text: https://github.com/Kotlin/KEEP/blob/master/proposals/jvm-records.md

cottand commented 3 years ago

Have you considered adding synthetic operator fun componentN() functions when seeing records from Kotlin? Like you say yourself, in record declarations the order of the compoents becomes significant. This would enable destructuring on records (like on data classes).

Maybe worth adding to the Future Improvements section

JakeWharton commented 3 years ago

That would negate the whole nominal aspect of records by introducing positional dependencies from Kotlin code. It means you could no longer add a property to the record anywhere but the end while retaining binary compatibility. This is the same problem that plagues data classes in public API today.

cottand commented 3 years ago

Java might go that direction anyway (with deconstruction patterns, which will be generated for records). It would probably be best wait to see what that will look like before trying to make it work from Kotlin. I agree it shouldn't be a thing straight away but I thought it was worth asking what the team's opinion was.

elizarov commented 3 years ago

@Cottand > Java might go that direction anyway...

It might, or it might not. We can wait and see. We can always add componentN if Java goes ahead with positional deconstruction, but removing what you have added is much harder.

JakeWharton commented 3 years ago

Brian Goetz indicated that deconstruction will likely be signature-based like a constructor when I asked him about it for my KotlinConf presentation last year: https://twitter.com/BrianGoetz/status/1200045322694729729.