On 21.11.24 11:37, 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
<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 am a bit late with my post, but maybe I can give a different angle to
this here.

There is of course the semantics of def and var being basically the same
as Object and all that, but there is also a different one.

def foo =  1

Can be seen as "define a variable with name foo and assign it the value 1"

var foo = 1

Can be seen as "declare variable foo with value 1". While

Object foo = 1

could be "declare a variable of name foo and type Object and assign it
the value 1"

All very similar.

def foo() {...} // define a method foo
String foo() {...} // define a method foo, which returns String
var foo() {..} //  define a method foo, which returns Object?

With def = define and var = variable you can build semantics that fit
well. But variable does not fit to property, attribute, field, parameter
or return type.


I think in the end that is about the only reason why var is used for
variables only.... or should be. In fact this makes an argument for not
using it for properties, as for example Eric already mentioned.

What do you think?

bye Jochen

Reply via email to