groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <blackd...@gmx.org>
Subject Re: Possible New Groovy Features... - var Keyword
Date Sun, 27 Aug 2017 11:22:36 GMT
On 27.08.2017 04:41, MG wrote:
[...]
> Flow typing is cool, and I would gather much harder to implement than a 
> "var" keyword (btw, I actually just used the name used by C#, even 
> though picking a different name might be a better choice, to avoid 
> confusion with e.g. Javascript).

flow typing is much harder to implement, yes, but existing (with bugs of 
course)

> I would however argue that in the majority of cases one will not want to 
> assign an integer value to a "String variable", but will want to keep 
> the type assigned at declaration time, so a var keyword to me is an 
> orthogonal feature to flow typing.

that is of course an argument I acknowledge

I should mention that if you do

Integer i = 1
i = "2"

you will still get a compilation error if type checking is enable, even 
with flow typing. The declaration type is like a minimum requirement for 
the type i can have during compilation. The only difference is, that the 
compiler can select a more specific type for i to allow method calls on 
the more specific type. Now if var and flow typing or orthogonal, then 
this means in

Map foo(){}
var i = foo()
i = new MySpecialMap()
i.methodExistingOnlyOnMySpecialMap()

we use Map as minimum type for i, but after the assignment with 
MySpecialMap, I would have a new specific type. And that means:

Map foo(){}
var i = new MySpecialMap()
i.methodExistingOnlyOnMySpecialMap()
i = foo()

would not compile.

Does this make sense for you?

bye Jochen

Mime
View raw message