Skip to content

Instantly share code, notes, and snippets.

@bodiam
Last active January 30, 2019 22:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bodiam/5795800 to your computer and use it in GitHub Desktop.
Save bodiam/5795800 to your computer and use it in GitHub Desktop.
A small description of my use case for Groovy data classes.
// 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; }
@bitsnaps
Copy link

bitsnaps commented Jan 30, 2019

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment