On 25.11.24 23:48, MG wrote:
Hi Jochen,
I am so glad that you are finally coming around to adding variable type
inference to Groovy ||-)
>
Here are some links to get anyone started that wants to work on this:
https://medium.com/javarevisited/local-variable-type-inference-in-java-use-of-var-59beb4f2c764
https://docs.oracle.com/en/java/javase/17/language/local-variable-type-inference.html
https://docs.oracle.com/javase/specs/jls/se10/html/jls-18.html
With var correctly inferring types from its RHS assignment, this can
finally also be added to final ||-)
ah...nah... we with flow typing, that kind of includes variable type
inference in the cases of var. A "real" type inference engine - going
beyond looking at both sides and then setting a type, no not in my plans
at the moment. I think that would require rewriting most of the compile
time type system, requiring it being fully static and have all relevant
types known at compile time. ;)
https://www.sciencedirect.com/science/article/pii/0167642395000070
we may have some short comings in lambda expressions.
But I have the feeling you know some cases I am not aware off fully,
that you would love to see fixed?
[...]
PS: I googled a bit, and I could not find in general that ppl think var
in Java is something you should avoid. Instead, as with any language
feature, one can over-/abuse it, thereby making code less readable than
when using explicit types.
well.. first thing I found is a guy wanting to write a sonar type rule
to very restrict the usage of var, a reddit discussion how var i... not
this one
https://www.reddit.com/r/java/comments/olmnnb/why_is_this_not_used_more_var_keyword/
but similar and so on. Ok, so maybe I overstated this a bit, but you
should use it with uttermost care.
And the funny thing is... I see that at myself. If I program in Groovy I
a using def mainly for local variables. If I program in Java, I actually
have to remind myself that var even exists. And even then I find the
Java code by far not as readable. But the big difference is not the
language, but the people I write the code with. There is a different
culture behind it and different processes. When it comes to reviewing
code in a complex application with the usual names, using var where
possible is not the best idea. I am part of the problem here, since I am
bad at giving names.
It is funny thinking about this in the context of Groovy, where
typically ppl who use it as a scripting language do not even use final
instead of def when defining variables, even though they will never
reassign said variable 99% of the time... ;-)
And funny thing... final does not go into the bytecode for local
variables either.
bye Jochen