News for hackers. Stuff that matters.
Re:Java's records, Lombok's data, and Kotlin's data classes by billyjobob on Friday May 07, 2021 @12:22PM (#27077593)
Kotlin data classes can use Java records as their implementation if running on JVM, so it’s not like you have to choose one or the other.

https://kotlinlang.org/docs/jvm-records.html#declare-records...

Re:Java's records, Lombok's data, and Kotlin's data classes by crdrost on Friday May 07, 2021 @03:23PM (#27079886)
This is great news!

I was also kind of surprised to see the article talking about algebraic data types... table stakes for ADTs is sum types and I have missed them considerably in the Java ecosystem.

Kotlin has something like them with sealed classes, although I'm a newcomer to the Kotlin and Spring Boot ecosystem so I don't see explicitly how I make some simple case like JSON

    {"type": "left", "abc": 123}
    {"type": "right", "def": "456"}
turn into some structure like:

    sealed class EitherTest {
        data class Left(val abc: Long): EitherTest()
        data class Right(val def: String): EitherTest()
    }
rather than the hacky version that you have to do in relational databases and Java which don't support such things,

    enum class EitherType { LEFT, RIGHT }
    data class EitherTest(val type: EitherType, val abc: Int?, val def: String?)
Like I’m not saying that there’s no way, I’m sure there’s a way... just that the ecosystem seems so hesitant to embrace sum types that like the above sealed class is widely viewed as a hack and there is no statement about “here is how you actually use sum types for everything in your Spring application with Kotlin.”

Was gonna give the choose-your-own-adventure example of why sum types are handy and how you have to kind of hack around them with inheritance when you don't have them but it occurs to me that anyone who has stuck with this comment this far probably already has some familiarity with this?

Re:Java's records, Lombok's data, and Kotlin's data classes by oftenwrong on Friday May 07, 2021 @04:20PM (#27080426)
Sealed classes are (most likely) going to be in the next version of Java, Java 17: https://openjdk.java.net/jeps/409
Re:Java's records, Lombok's data, and Kotlin's data classes by aphexairlines on Friday May 07, 2021 @09:51PM (#27082896)
Re:Java's records, Lombok's data, and Kotlin's data classes by TJSomething on Friday May 07, 2021 @03:40PM (#27080073)
In traditional Java, you're probably using Jackson for your JSON. You can achieve those sorts of results by specifying the classes and type tags in annotations on an abstract Either class to use Jackson's polymorphic serialization feature.
Re:Java's records, Lombok's data, and Kotlin's data classes by balefrost on Friday May 07, 2021 @04:04PM (#27080299)
The point of sealed classes is that you have a known number of possible representations. So you can have `when` blocks that exhaustively check all possibilities. Having an open class hierarchy would not work for that purpose.
Re:Java's records, Lombok's data, and Kotlin's data classes by __jem on Friday May 07, 2021 @09:17PM (#27082722)
this technique works with sealed classes, i’ve done it before. it throws a runtime exception if it can’t identify the discriminant.
Re:Java's records, Lombok's data, and Kotlin's data classes by winrid on Saturday May 08, 2021 @03:14AM (#27084715)
The easiest way is with GSON:

https://mkyong.com/java/how-do-convert-java-object-to-from-j...

You would just have to have annotations on the enum names to get the lowercase values in your example json.

Re:Java's records, Lombok's data, and Kotlin's data classes by _old_dude_ on Friday May 07, 2021 @02:55PM (#27079509)
I wonder why, unlike a Java record, in Kotlin a data class annotated with @Record can not be local.

Being able to use local records is a useful feature in particular in unit tests.

  @Test
  void testPoints() {
     record Point(int x, int y) {}
     ...
  }
Re:Java's records, Lombok's data, and Kotlin's data classes by tadfisher on Friday May 07, 2021 @10:11PM (#27083033)
I don't see the point; you're not testing code that depends on Point, as it's local, so `x to y` (i.e. `Pair(x, y)`) would work just as well.

If there's a particular language feature records enable that Kotlin doesn't with ordinary data classes in this case, I'd love to see it.

Re:Java's records, Lombok's data, and Kotlin's data classes by nicolai-parlog on Saturday May 08, 2021 @12:40PM (#27088675)
That misses the point. The strength of records is that the language can build features that are only possible because of records' restrictions. It's not that the bytecode says "I'm a record" - which is all you get if Kotlin compiles a data class to one. @JvmRecord gives you all the limitations with none of the benefits. The annotation only exists for compatibility reasons.
Re:Java's records, Lombok's data, and Kotlin's data classes by unscaled on Sunday May 09, 2021 @12:45AM (#27093302)
The problem is all of these "benefits" are imaginary right now[1]. You are talking about future possibilities that Java could get due the strong guarantees offered by Java records. That's cool, but I couldn't find in your article (or the JEPs) any such benefit that is available _right now_.

The reality is that Kotlin can build the very same benefits you're talking about as well, in future versions. All the compiler has to do is to restrict these benefits to data classes which only have 'val' members, do not have hidden state and do not use inheritance. In fact, Kotlin is ALREADY enforcing these constraints on a any data class that you annotate with @JvmRecord.

Now, if we're already talking about imaginary future benefits here, Kotlin is also planning to have (immutable by design) value classes when Project Valhalla is ready (you can already have value classes with a single field in Kotlin 1.5).

[1] To be fully honest, Java records themselves are themselves an imaginary feature for the vast majority of Java users until Java 17 comes out, since very few projects would use a non-LTS version. And even then, you can expect years until libraries can start to use them. Kotlin and Lombok data classes can be used right now.

Re:Java's records, Lombok's data, and Kotlin's data classes by pjmlp on Saturday May 08, 2021 @04:01AM (#27084974)
Yeah, and that is a good example of how semantics might change depending on which platform Kotlin code is targeting.