I thought Gianluca made an excellent argument and helps modernize the language. Given Groovy’s symbiotic relationship with Java, it seems like the guidance here should probably be to follow Java’s usage as that likely keeps friction/confusion to a minimum.
Not being a Java developer, it isn’t clear to me whether Java supports var as a return type though I assume it does for consistency. On Nov 21, 2024, at 11:10, Milles, Eric (TR Technology) via dev <dev@groovy.apache.org> wrote:
I don't think semantically that "var name() { ... }" makes sense.
One might argue that var for field and property do not make sense either. We could explore removing support for var on class members.
From: Gianluca Sartori <g.sart...@gmail.com>
Sent: Thursday, November 21, 2024 10:57 AM
To: dev@groovy.apache.org <dev@groovy.apache.org>
Subject: [EXT] Re: Using `var` as method return type placeholder
External Email: Use caution with links and attachments. |
Well, actually that's not true, Groovy supports creating fields and properties as well with `var`, so basically everything `def` does except return types.
Gianluca Sartori
--
Cell. +39 388 1026822
Hi Gianluca,
`var` was introduced to Groovy just for the better compatibility of Java. Java just supports declaring variables with `var`, so does Groovy.
Cheers,
Daniel Sun
On 2024/11/21 10:37:23 Gianluca Sartori wrote:
> Hello everybody,
>
> My name is Gianluca Sartori, from Italy, I am the author of the open source
> project Dueuno Elements (https://github.com/dueuno-projects/dueuno-elements)
> and I am new to this list.
>
> I would like to start using the more Java-ish `var` instead of the
> Python-ish `def` lexicon but I came across the fact that I cannot use `var`
> as method return type placeholder.
>
> My understanding is that I can use `var` for both local variables and class
> fields/properties but I cannot use it, for example, if I want to have a
> read only property. The code below does not compile:
>
> class C {
> var firstname
> var lastname
>
> var getFullname() {
> return firstname + ' ' + lastname
> }
> }
>
> var c = new C(firstname: 'Gianluca', lastname: 'Sartori')
> c.fullname
>
> I'd like to switch to using `var` as a type placeholder, but having to use
> `var` for variable declaration and keep using `def` for methods definition
> is something I don't understand. I love Groovy because it is easy. This
> restriction of the `var` type placeholder forces me to write code that
> mixes Python lexicon the new Java lexicon.
>
> My main worry is with Grails controllers where we need to define an action
> using `def` (or `Object`) as return type and define variables in the method
> body. At the moment we have the following options:
>
> *def* index() {
> *def* myVar = ...
> }
>
> *def* index() {
> *var* myVar = ...
> }
>
> *def* index() {
> *Object* myVar = ...
> }
>
> *Object* index() {
> *def* myVar = ...
> }
>
> *Object* index() {
> *var* myVar = ...
> }
>
> *Object* index() {
> *Object* myVar = ...
> }
>
> I would like to write controllers like this:
>
> *var* index() {
> *var* myVar = ...
> }
>
> to keep the code clean and coherent with the Groovy documentation that
> states clearly that *"If you think of def and var as an alias of Object,
> you will understand in an instant."*
>
> Is this enough of an argument to ask for an implementation of `var` that is
> fully intrechangable with `def`?
>
> Please let me know what is your opinion on that,
> cheers,
> Gianluca
>
> Gianluca Sartori
> --
> Cell. +39 388 1026822
>
|