If you think of def and var as an alias of Object, you will understand in an
instant.
I too think this line should be replaced. There are circumstances where def only means
"define" and is not a type placeholder.
multi-variable declaration: def (a, int b, char c) = [0,1,'2'] // def is not
synonymous with Object
I would argue that def for method/function/procidure declaration also means
"define"
________________________________
From: o...@ocs.cz <o...@ocs.cz>
Sent: Tuesday, December 10, 2024 8:49 AM
To: Groovy_Developers <dev@groovy.apache.org>
Cc: Paul King <pa...@asert.com.au>; MG <mg...@arscreat.com>
Subject: Re: [EXT] Using `var` as method return type placeholder
Ah, I though we are discussing the real usage/support, not just the
documentation (which of course can be always improved).
Most probably the proper solution would be change the documentation to
something more-or-less like
===
[def explained without mentioning var] Note that for a seamless compatibility
with a Java legacy code, you can also use var, which can be placed where Java
allows it (typically variable declarations) and where applicable, it is fully
equivalent to def.
===
Myself, I would also add something like
===
Although var does work and will work in future all right, when (if ever) the
code is “groovyfied” by removing semicolons, replacing for(:) by for/in etc.,
we also recommend to replace vars by defs for consistence (there is no
functional difference though; if you like var personally, feel free to stick
with it, just like you can freely stick with semicolons).
===
Incidentally, myself, I don't like the “Object alias” part. To me it seems
rather misleading. Frankly I don't know how it currently works (and what
changes, if any, are presumed for future), but in my personal opinion there's a
subtle but important difference: def is just about anything (and should support
type inference far as reasonably possible). Object is an instance of the Object
class (and whether it should infer types even if possible or not is highly
arguable). Nevertheless this path leads to a need to support something like
ObjC __kindof, which Groovy (far as I know) currently has not, along with
better dynamic typechecking etc., and would open another big can of worms.
To me, nothing of this seems too important though (and had I understood we are
debating docs only, I would not write my previous message quoted below). Even
if the documentation is imperfect (any documentation always is), it gives a
good overall idea how things work; the fine details can be easily learnt later.
Thanks and all the best,
OC
On 10. 12. 2024, at 15:21, Gianluca Sartori <g.sart...@gmail.com> wrote:
I might be wrong and I might have misunderstood something of importance, but it
seems to me there is one (and only one) very specific need to use var: ensuring
a code copy/pasted from Java works (with as little number of problems as
reasonably possible).
This is what I understood as well reading the docs, but it looks like `var` is
starting to become a full part of the Groovy syntax because it has been
documented along with `def` as equal to `def` (but only for variable
definitions).
I would like to use `var` as a return type placeholder meaning "this method returns
a variable"
What does it mean? Far as I know (but of course, my knowledge is limited),
there's no var-ref type in Java (nor Groovy), and thus no method can return a
variable. Have I missed something of importance again?
You have missed that part in the docs where it says:
If you think of def and var as an alias of Object, you will understand in an
instant.
So if `var` is an alias for `Object` one should be able to use it as a type
placeholder in method return types as well, but that's not the case