groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From MG <mg...@arscreat.com>
Subject Re: JDK 10: Use of var over def?
Date Fri, 23 Mar 2018 17:14:50 GMT
Hi Paul,

wouldn't it make sense to combine flow typing with  var x = RHS being 
identical to  typeof(RHS) x = RHS  :

@Canonical static class Foo {
   int x }

@InheritConstructors static class SonOfFooextends Foo {
   int sonOfFooMethod() {2*x }
}


@Test @Ignore @CompileStatic void flowTypedVar() {
   SonOfFoo f =new SonOfFoo(21)
   //f = new Foo(-1) // compile time fails with "Groovyc: [Static type 
checking] - Cannot assign value of type groovy.GroovyGeneralSpike$Foo to 
variable of type groovy.GroovyGeneralSpike$SonOfFoo" 
//f.sonOfFooMethod() // compile time fails with "Groovyc: [Static type 
checking] - Cannot find matching method 
groovy.GroovyGeneralSpike$Foo#sonOfFooMethod()." if(finstanceof SonOfFoo) {
     println f.sonOfFooMethod()// works because of flow typing }
}

Cheers,
mg



On 23.03.2018 15:42, Paul King wrote:
> The Parrot parser already has support for this at the grammar level 
> but we regard some of the current implementation details as experimental.
>
> At the moment it is almost just an alias for "def" but discussions 
> have been around whether we can make the behavior closer to Java when 
> used within static Groovy code. We haven't defined exactly what this 
> would mean yet but roughly I suspect it could mean the same 
> inferencing as now but without flow typing.
>
> Cheers, Paul.
>
>
> On Fri, Mar 23, 2018 at 10:12 PM, Merlin Beedell 
> <MBeedell@cryoserver.com <mailto:MBeedell@cryoserver.com>> wrote:
>
>     I see that the newly release JDK 10 now supports the “var”
>     declaration for local variables where the type can clearly be
>     inferred from its initialiser:
>
>     http://openjdk.java.net/jeps/286 <http://openjdk.java.net/jeps/286>
>
>     I note that Groovy’s “def” syntax (among others) was mentioned but
>     rejected.
>
>     Would Groovy move to support this syntax in the same way (support
>     ‘var’ only for Type Safe inferred declarations) or as a general
>     alias to the “def” keyword?
>
>     JDK 10 also has support for “docker” containers.  The ecosystem
>     has certainly shifted!
>
>     Merlin Beedell
>
>


Mime
View raw message