This is an automated email from the ASF dual-hosted git repository. sunlan pushed a commit to branch GROOVY_3_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git
commit dce33a30eeb0aae6ffa3b232366eed6347682b0a Author: Daniel Sun <sun...@apache.org> AuthorDate: Wed Jan 15 14:59:08 2020 +0800 Add docs for Java-style non-static inner class instantiation (cherry picked from commit 2533b24016893466df290cede7c2be8566913f79) --- src/spec/doc/core-differences-java.adoc | 5 ++++- src/spec/doc/core-object-orientation.adoc | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/spec/doc/core-differences-java.adoc b/src/spec/doc/core-differences-java.adoc index 1170704..d61c6f1 100644 --- a/src/spec/doc/core-differences-java.adoc +++ b/src/spec/doc/core-differences-java.adoc @@ -186,7 +186,7 @@ In Java you can do this: include::{projectdir}/src/spec/test/DifferencesFromJavaTest.groovy[tags=innerclass_3_java,indent=0] ---------------------------------- -Groovy doesn't support the `y.new X()` syntax. Instead, you have to write `new X(y)`, like in the code below: +Before 3.0.0, Groovy doesn't support the `y.new X()` syntax. Instead, you have to write `new X(y)`, like in the code below: [source,groovy] ---------------------------------- @@ -201,6 +201,9 @@ There is a danger that you will write new X() instead of new X(this) for example. Since this might also be the regular way we have not yet found a good way to prevent this problem. +[NOTE] +Groovy 3.0.0 supports Java style syntax for creating instances of non-static inner classes. + == Lambda expressions and the method reference operator Java 8+ supports lambda expressions and the method reference operator (`::`): diff --git a/src/spec/doc/core-object-orientation.adoc b/src/spec/doc/core-object-orientation.adoc index 0ff7be4..a4a3657 100644 --- a/src/spec/doc/core-object-orientation.adoc +++ b/src/spec/doc/core-object-orientation.adoc @@ -144,6 +144,22 @@ include::{projectdir}/src/spec/test/ClassTest.groovy[tags=inner_class2,indent=0] Note that the class `Inner2` is defined only to provide an implementation of the method `run` to class `Outer2`. Anonymous inner classes help to eliminate verbosity in this case. +Since Groovy 3.0.0, Java syntax for non-static inner class instantiation is now supported, for example: + +[source,groovy] +-------------------------------------- +class Computer { + class Cpu { + int coreNumber + + Cpu(int coreNumber) { + this.coreNumber = coreNumber + } + } +} + +assert 4 == new Computer().new Cpu(4).coreNumber +-------------------------------------- ===== Anonymous inner class