Hi Gianluca,
1. I fully agree with the documentation bit, and would also think it
better if Java-syntax-compatibility Groovy constructs would be
separated from idiomatic Groovy in e.g. a "Java syntax in Groovy"
section :-)
1. With some constructs the only harm done is that ppl new to
Groovy might not use the most groovy way to write Groovy code,
but for var it is really a bad idea, because if ppl start to use
it as an alias for Object the same as def, switching the
semantics to type inference could then break some existing
Groovy code.
2. Do you maybe have a link to the "using 'var' in Groovy code" talk
you mention ?
Cheers,
mg
On 26/11/2024 10:10, Gianluca Sartori wrote:
I have the feeling that your confusion comes from the fact that
you think Groovy = Python in the Java world, and Python def = Java
var, therefore Groovy var should be def.
Well, my confusion comes from the documentation
(https://groovy-lang.org/semantics.html#_variable_definition) that states:
> If you think of |def| and |var| as an alias of |Object|, you will
understand in an instant.
So I understood the wrong thing instantly :)
There are tons of constructs Groovy supports for the sole reason
to be as copy & paste compatible with Java as possible (Java-style
curly-braces literal arrays, Java for-each loops, etc, etc), and
none of these are first class citizens / idiomatic Groovy.
The var reserved word, that as a technicality is currently mapped
to def with some restrictions, is just a small blip in a large sea
here.
Okay then I guess that this should be clear from the documentation.
Because they can be seen as new features of the language we should use
to stay updated. I mean we should not even talk about `var` in the
same place we are documenting `def`, there should be a separate
chapter "Compatibility with Java".
I use `def` everywhere when writing the higher/final layer of my
software (Eg, not the internal API layers where I prefer to use types
to self document the code) this `var` topic I am discussing here was
triggered by a talk I've heard about starting to use 'var' in Groovy code.
Cheers,
Gianluca