You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/02/14 07:40:27 UTC

[groovy] branch master updated: GROOVY-8582: Documentation for `var` keyword (Java10)

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 03802d0  GROOVY-8582: Documentation for `var` keyword (Java10)
03802d0 is described below

commit 03802d06e67bb21aa5e600a2832ee26cc0a1b930
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Feb 14 17:40:14 2020 +1000

    GROOVY-8582: Documentation for `var` keyword (Java10)
---
 src/spec/doc/core-closures.adoc           |  2 +-
 src/spec/doc/core-object-orientation.adoc | 12 +-----------
 src/spec/doc/core-semantics.adoc          | 15 ++++++++++++---
 src/spec/doc/core-syntax.adoc             |  3 ++-
 src/spec/test/ClassTest.groovy            | 18 ++++++++++++++++++
 src/spec/test/SemanticsTest.groovy        |  3 ++-
 6 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/src/spec/doc/core-closures.adoc b/src/spec/doc/core-closures.adoc
index 43f86a9..8624b9b 100644
--- a/src/spec/doc/core-closures.adoc
+++ b/src/spec/doc/core-closures.adoc
@@ -85,7 +85,7 @@ other variable, despite being a block of code:
 include::{projectdir}/src/spec/test/ClosuresSpecTest.groovy[tags=closure_is_an_instance_of_Closure,indent=0]
 ----
 <1> You can assign a closure to a variable, and it is an instance of `groovy.lang.Closure`
-<2> If not using `def`, you can assign a closure to a variable of type `groovy.lang.Closure`
+<2> If not using `def` or `var`, use `groovy.lang.Closure` as the type
 <3> Optionally, you can specify the return type of the closure by using the generic type of `groovy.lang.Closure`
 
 === Calling a closure
diff --git a/src/spec/doc/core-object-orientation.adoc b/src/spec/doc/core-object-orientation.adoc
index a4a3657..5121796 100644
--- a/src/spec/doc/core-object-orientation.adoc
+++ b/src/spec/doc/core-object-orientation.adoc
@@ -148,17 +148,7 @@ Since Groovy 3.0.0, Java syntax for non-static inner class instantiation is now
 
 [source,groovy]
 --------------------------------------
-class Computer {
-    class Cpu {
-        int coreNumber
-
-        Cpu(int coreNumber) {
-            this.coreNumber = coreNumber
-        }
-    }
-}
-
-assert 4 == new Computer().new Cpu(4).coreNumber
+include::{projectdir}/src/spec/test/ClassTest.groovy[tags=inner_instantiation,indent=0]
 --------------------------------------
 
 ===== Anonymous inner class
diff --git a/src/spec/doc/core-semantics.adoc b/src/spec/doc/core-semantics.adoc
index bfcc72c..7dec41d 100644
--- a/src/spec/doc/core-semantics.adoc
+++ b/src/spec/doc/core-semantics.adoc
@@ -27,16 +27,25 @@ This chapter covers the semantics of the Groovy programming language.
 
 === Variable definition
 
-Variables can be defined using either their type (like `String`) or by using the keyword `def`:
+Variables can be defined using either their type (like `String`) or by using the keyword `def` (or `var`) followed by a variable name:
 
 [source,groovy]
 ----
 include::{projectdir}/src/spec/test/SemanticsTest.groovy[tags=variable_definition_example,indent=0]
 ----
 
-`def` is a replacement for a type name. In variable definitions it is used to indicate that you don't care about the type. In variable definitions it is mandatory to either provide a type name explicitly or to use "def" in replacement. This is needed to make variable definitions detectable for the Groovy parser.
+`def` and `var` act as a type placeholder, i.e. a replacement for the type name,
+when you do not want to give an explicit type.
+It could be that you don't care about the type at compile time
+or are relying on type inference (with Groovy's static nature).
+It is mandatory for variable definitions to have a type or placeholder.
+If left out, the type name will be deemed to refer to an existing variable (presumably declared earlier).
+For scripts, undeclared variables are assumed to come from the Script binding.
+In other cases, you will get a missing property (dynamic Groovy) or compile time error (static Groovy).
+If you think of `def` and `var` as an alias of `Object`, you will understand in an instant.
 
-You can think of `def` as an alias of `Object` and you will understand it in an instant.
+Variable definitions can provide an initial value,
+in which case it's like having a declaration and assignment (which we cover next) all in one.
 
 [NOTE]
 Variable definition types can be refined by using generics, like in `List<String> names`.
diff --git a/src/spec/doc/core-syntax.adoc b/src/spec/doc/core-syntax.adoc
index 416643e..2742574 100644
--- a/src/spec/doc/core-syntax.adoc
+++ b/src/spec/doc/core-syntax.adoc
@@ -160,7 +160,8 @@ The following list represents all the keywords of the Groovy language:
 |true
 |try
 
-|while | | |
+|var
+|while | |
 
 |===
 
diff --git a/src/spec/test/ClassTest.groovy b/src/spec/test/ClassTest.groovy
index 6264be7..177c575 100644
--- a/src/spec/test/ClassTest.groovy
+++ b/src/spec/test/ClassTest.groovy
@@ -41,6 +41,24 @@ class ClassTest extends GroovyTestCase {
         '''
     }
 
+    void testInnerInstantiation() {
+        assertScript '''
+            // tag::inner_instantiation[]
+            class Computer {
+                class Cpu {
+                    int coreNumber
+            
+                    Cpu(int coreNumber) {
+                        this.coreNumber = coreNumber
+                    }
+                }
+            }
+
+            assert 4 == new Computer().new Cpu(4).coreNumber
+            // end::inner_instantiation[]
+        '''
+    }
+
     void testInnerClass() {
         assertScript '''
             // tag::inner_class[]
diff --git a/src/spec/test/SemanticsTest.groovy b/src/spec/test/SemanticsTest.groovy
index 2c2c26f..cc0e9a1 100644
--- a/src/spec/test/SemanticsTest.groovy
+++ b/src/spec/test/SemanticsTest.groovy
@@ -24,7 +24,8 @@ class SemanticsTest extends CompilableTestSupport {
     void testVariableDefinition() {
         // tag::variable_definition_example[]
         String x
-        def o
+        def y
+        var z
         // end::variable_definition_example[]
     }