You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by MG <mg...@arscreat.com> on 2017/08/22 22:52:21 UTC

Re: Possible New Groovy Features... - var Keyword

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.

Cheers,
Markus



Re: Possible New Groovy Features... - var Keyword

Posted by Jochen Theodorou <bl...@gmx.org>.
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

Re: Possible New Groovy Features... - var Keyword

Posted by MG <mg...@arscreat.com>.
Hi Jochen,

On 25.08.2017 10:05, Jochen Theodorou wrote:
>
>
> 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

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).
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.

> 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.

Emphasis on "if possible"... :-)
Var would be for the remaining cases where variable reassignment is the 
most sensible approach.

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

see under "orthogonal feature" above; var would work in dynamically as 
well as statically compiled code, so it is also orthogonal in that sense.

Cheers,
Markus






Re: Possible New Groovy Features... - var Keyword

Posted by Jochen Theodorou <bl...@gmx.org>.

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

Re: Possible New Groovy Features... - var Keyword

Posted by Paul King <pa...@asert.com.au>.
The same argument applies to this as to the 'the type of final fields from
their assigned value' case.
So, possible perhaps within @CompileStatic contexts.

On Wed, Aug 23, 2017 at 8:52 AM, MG <mg...@arscreat.com> 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.
>
> Cheers,
> Markus
>
>
>