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

sunlan 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 9131ca7ab3 improve explanation of `var`
9131ca7ab3 is described below

commit 9131ca7ab3305ed690a78cfef56248b837c6b1ec
Author: Paul King <[email protected]>
AuthorDate: Wed Dec 11 11:28:23 2024 +1000

    improve explanation of `var`
---
 src/spec/doc/core-semantics.adoc   | 24 ++++++++++++++++++++----
 src/spec/test/SemanticsTest.groovy |  6 ++++--
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/src/spec/doc/core-semantics.adoc b/src/spec/doc/core-semantics.adoc
index 8a74339de5..e528523b74 100644
--- a/src/spec/doc/core-semantics.adoc
+++ b/src/spec/doc/core-semantics.adoc
@@ -39,14 +39,14 @@ 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` (or `var`) followed by a variable name:
+Variables can be defined using either their type (like `String`) or by using 
the keyword `def` followed by a variable name:
 
 [source,groovy]
 ----
-include::../test/SemanticsTest.groovy[tags=variable_definition_example,indent=0]
+include::../test/SemanticsTest.groovy[tags=variable_definition_example1,indent=0]
 ----
 
-`def` and `var` act as a type placeholder, i.e. a replacement for the type 
name,
+`def` acts 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).
@@ -54,7 +54,7 @@ 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.
+If you think of `def` as an alias of `Object`, you will understand 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.
@@ -63,6 +63,22 @@ in which case it's like having a declaration and assignment 
(which we cover next
 Variable definition types can be refined by using generics, like in 
`List<String> names`.
 To learn more about the generics support, please read the 
<<{core-object-orientation}#generics,generics section>>.
 
+[NOTE]
+--
+Java introduced the `var` reserved type from Java 10.
+It also acts like a type placeholder for variable definitions, similar to 
`def` above.
+So, for compatibility with Java,
+Groovy also lets you define variables using `var` as follows:
+[source,groovy]
+----
+include::../test/SemanticsTest.groovy[tags=variable_definition_example2,indent=0]
+----
+In the context of variable definitions, you can think of `var` as an alias for 
`def`.
+You might use `var` if you have cut-n-pasted some Java code into your codebase,
+or if the audience (readers or maintainers) of your codebase are primarily
+Java-aware folks, and you want to make the code look familiar to them.
+--
+
 === Variable assignment
 
 You can assign values to variables for later use. Try the following:
diff --git a/src/spec/test/SemanticsTest.groovy 
b/src/spec/test/SemanticsTest.groovy
index e991913ad9..52e587c74b 100644
--- a/src/spec/test/SemanticsTest.groovy
+++ b/src/spec/test/SemanticsTest.groovy
@@ -22,11 +22,13 @@ import groovy.transform.Immutable
 class SemanticsTest extends CompilableTestSupport {
 
     void testVariableDefinition() {
-        // tag::variable_definition_example[]
+        // tag::variable_definition_example1[]
         String x
         def y
+        // end::variable_definition_example1[]
+        // tag::variable_definition_example2[]
         var z
-        // end::variable_definition_example[]
+        // end::variable_definition_example2[]
     }
 
     void testVariableAssignment() {

Reply via email to