Hi Paul,
1. If I will assign a value to a variable later, I would use var with
Java semantics. Without var I give the type of the variable
explicitely as restrictive as possible. I would not use def (I only
use def as the return value for a method returning an anonymous
class instance, since IntelliJ Intellisense picks up the type in
that case, contrary to when using Object as the return type).
2. If I will not assign a value to a variable later on, I use final.
(Jochen's argument, as I understood it, was, that my idea of var (close
to what Java will do) differs from Groovy's def only when assigning a
value to the variable later on. To which I replied that in my book that
is in 100% of all cases (because otherwise I use final)).
In code:
@Canonical static class FoorchterlichLongerNome { Numberx; Strings }
@Test void finalFooVarTest() {
final x =new FoorchterlichLongerNome(123,'abc')
println"x=$x" x =987 println"x=$x" }
Compile time error:
Error:(23, 5) Groovyc: The variable [x] is declared final but is reassigned
@Test void fooVarTest() {
FoorchterlichLongerNome x =new FoorchterlichLongerNome(123,'abc')
//var x = new FoorchterlichLongerNome(123,'abc') // equivalent to the
above line in var with Java semantics println"x=$x" x =new FoorchterlichLongerNome(4567,'DEFG')
println"x=$x" x =987 println"x=$x" }
Runtime error:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot
cast object '987' with class 'java.lang.Integer' to class
'groovy.Groovy_var_keyword_Spike$FoorchterlichLongerNome'
@Test void defFooVarTest() {
def x =new FoorchterlichLongerNome(123,'abc')
//var x = new FoorchterlichLongerNome(123,'abc') // equivalent to the
above line in var with Groovy def semantics println"x=$x" x =987 println"x=$x" }
Output:
x=groovy.Groovy_var_keyword_Spike$FoorchterlichLongerNome(123, abc)
x=987
On 08.03.2018 23:42, Paul King wrote:
So in that one aspect of "assigning a value later on" your expectation
is exactly like Java's "var" and Groovy's current "def"?
On Thu, Mar 8, 2018 at 11:58 PM, mg <mg...@arscreat.com
<mailto:mg...@arscreat.com>> wrote:
My argument was not in relation to the JEP, but a Groovy user
story, in relation to you saying, that I would not see a
difference between def and var, apart from when assigning a value
later on.
But assigning a value later on is _exactly_ what I am going to do
when I use var - because otherwise I would use final instead of var...
-------- Ursprüngliche Nachricht --------
Von: Jochen Theodorou <blackd...@gmx.org <mailto:blackd...@gmx.org>>
Datum: 08.03.18 13:32 (GMT+01:00)
An: dev@groovy.apache.org <mailto:dev@groovy.apache.org>
Betreff: Re: About supporting `var` of Java10+
Am 08.03.2018 um 12:45 schrieb mg:
> Maybe I am missing your point, but what I meant was: When I use
>
> var x = new Foo()
>
> I indicate that x will be reassigned further down in the scope,
> otherwise I use
>
> final x = new Foo()
That's what I understood. But the later variant is not part of the
JEP.
In Groovy what you wrote is an alias for final Object x = new Foo()
bye Jochen