Skip to content

Instantly share code, notes, and snippets.

@Mahoney
Last active October 18, 2018 21:13
Show Gist options
  • Select an option

  • Save Mahoney/315c7dd5a10d4c6786ed19a1ecb49d40 to your computer and use it in GitHub Desktop.

Select an option

Save Mahoney/315c7dd5a10d4c6786ed19a1ecb49d40 to your computer and use it in GitHub Desktop.
Shows how Groovy allows delegating without exposing the encapsulated type
@CompileStatic
class MyString {
@Delegate
private final String decorated
MyString(String decorated) {
this.decorated = decorated
}
}
@CompileStatic
class Test {
static void main(String[] args) {
def m = new MyString("blah")
assert m.startsWith("bl") // compiles, passes
assert !(m instanceof String) // passes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment