Last active
January 30, 2019 22:05
-
-
Save bodiam/5795800 to your computer and use it in GitHub Desktop.
A small description of my use case for Groovy data classes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Current way in Groovy | |
@Immutable | |
class Person { | |
String firstName, lastName | |
int age | |
Date dateCreated | |
} | |
// But: This would be nice | |
data class Person(String name, String lastName, int age, Date dateCreated) | |
// or maybe | |
data class Person(String name, lastName, int age, Date dateCreated) // omits types when they are the same | |
// Motivation | |
// The above should create a class which would create a similar construct as Scala case classes or Kotlin data classes | |
// http://www.scala-lang.org/node/258 | |
// @Immutable is similar, but it's more typing. | |
// The closest I can get in Groovy is this: | |
@Immutable class Person { String firstName, lastName, city, int age; Date datecreated; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had the same thought, I find data class very concise way to make a full bean class without boilerplate, then I realized that groovy has many AST's to do the similar thing without breaking the Java-Like syntax, I'm not sure if we mix
@Canonical
+@Immutable
+@TupleConstructor
would give us a better@Data
class, or maybe @paulk-asert has already done something like that to Groovy v3 :)