groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From MG <>
Subject Re: About supporting `var` of Java10+
Date Fri, 09 Mar 2018 00:01:48 GMT
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 

@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" }

x=groovy.Groovy_var_keyword_Spike$FoorchterlichLongerNome(123, abc)

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 < 
> <>> 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 < <>>
>     Datum: 08.03.18 13:32 (GMT+01:00)
>     An: <>
>     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

View raw message