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 Fri, 25 Aug 2017 08:05:52 GMT


On 23.08.2017 00:52, MG wrote:
> Hi Paul,
>
> On 21.08.2017 04:30, Paul King wrote:
>>
>>     Introduce a "var" (o.s.) keyword that allows deduction of type
>>     through assignment:
>>     var device = new PrinterDevice(...) // device variable will have
>>     type PrinterDevice without the need to explictely state that
>>     Rationale: This is a well known feature of other languages, that
>>     reduces the need to explictely define the type of variables.
>>
>
>> How is this different to the current type inferencing?
>
> The variable/field will have the type of the initally assigned value
> (instead of Object when using def), therefore one cannot accidentially
> assign any other type to it.


Didn't see a further comment on this so... If you have the static 
compilation mode we use flow typing to determine the type of the 
variables. That means:

def x = ""

will make x have the type compile time type String, so that x.foo() will 
be marked as compilation error. Flow typing means you can do this:

def x = ""
x = 1

without getting an error. Basically you loose compile time checks on 
assignment and gain more flexibility on the usage of the variable. But 
since you seem to want to make everything final if possible, that should 
not bother you too much.

The bottom line is: var kind of the default in static compilation mode

bye Jochen

Mime
View raw message