Skip to content

Instantly share code, notes, and snippets.

@Dierk
Created April 8, 2014 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dierk/10189588 to your computer and use it in GitHub Desktop.
Save Dierk/10189588 to your computer and use it in GitHub Desktop.
Using Groovy traits with @DeleGate AST transform
// has all methods of List
trait ListLike {
@Delegate List linkedList = new LinkedList()
}
// stores anything but exposes only Strings
class StringList implements ListLike {
String get(int index) { linkedList.get(index).toString() }
}
ListLike stringList = new StringList()
stringList << 0 // adding an int
assert '0' == stringList[0] // getting a String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment