As an avid final user, I would vote +1 in principle, with the following caveats : -)

1. I am alas not fond of either the "val" or the "let" keyword
   (explanation upcoming).
2. Groovy has @AutoFinal
   
(https://docs.groovy-lang.org/latest/html/gapi/groovy/transform/AutoFinal.html)
   - while we decided not to apply this setting to local variables or
   fields, we could rethink that, and allow it to apply in these cases
   also (maybe through an annotation parameter). In turn we would then
   want to make it possible to mark a field/variable to be non-final,
   which could be done through a keyword of its own (similar to transient).
3. To my knowledge "var" in Groovy is still only an alias to "def", so
   "final var" is equivalent to just "final". Introducing another
   syntax here seems confusing/overkill, but if we do it, I repropose
   introducing "fin" as an alias for "final", together with the change
   to make final actually equivalent to "final var" in Java (i.e. make
   the variable/field finally have its RHS type instead of using Object
   + flow typing, which is not the same in practice).

Cheers,
mg


On 28/04/2020 14:09, mojo2012 wrote:
Dear development community,

I'm a long time java developer and I'm used to add "final" to my variable
definition: final var name = "a"

I think the *"var"* keyword (and the old "*def*" keyword) itself helps a
lot, but seeing "*var*" and "*final var*" not being aligned properly in code
makes me crazy.

As we have def and var in groovy, wouldn't it be nice to have a val or let
in groovy too, that is just a short form of "final var"?

Although personally I would prefer "*val*" I do understand that some people
might have problems differentiating "*var*" and "*val*", hence the "*let*".
Both *Kotlin* and *Scala* went for the "val" keyword as well - so it might
not bee too bad.

Anyway I created a JIRA ticket for this:
https://issues.apache.org/jira/browse/GROOVY-9308
And I created a *patch PR*: https://github.com/apache/groovy/pull/1236

The patch as of now contains both the "let" and "val" syntax. I added tests
to check if the actual variable is final. Those tests all seem find. But I
wasn't able to see "final val" as syntax error (so the test currently
fails).

I'm looking forward to get some feedback from you (hopefully positive :-))

[ ] +1 Add either val or let as new shortcut for "final def"/"final var"
[ ]  0 I don't have a strong opinion about this, but I assume it's ok
[ ] -1 I don't like this feature because ...





--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Reply via email to