You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by "Milles, Eric (TR Technology) via dev" <de...@groovy.apache.org> on 2024/12/10 14:58:31 UTC
Re: Using `var` as method return type placeholder
> 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: ocs@ocs.cz <oc...@ocs.cz>
Sent: Tuesday, December 10, 2024 8:49 AM
To: Groovy_Developers <de...@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....@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
Re: Using `var` as method return type placeholder
Posted by Gianluca Sartori <g....@gmail.com>.
Love it
Gianluca Sartori
--
Cell. +39 388 1026822
On Wed, 11 Dec 2024 at 02:33, Paul King <pa...@asert.com.au> wrote:
> I created this pull request which clarifies things somewhat:
>
> https://github.com/apache/groovy/pull/2133
>
> But feel free to create an alternative if you think it can be improved
> further.
>
> Cheers, Paul.
>
> On Wed, Dec 11, 2024 at 12:58 AM Milles, Eric (TR Technology)
> <er...@thomsonreuters.com> wrote:
> >
> > > 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: ocs@ocs.cz <oc...@ocs.cz>
> > Sent: Tuesday, December 10, 2024 8:49 AM
> > To: Groovy_Developers <de...@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....@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
> >
> >
> >
> >
>
Re: Using `var` as method return type placeholder
Posted by MG <mg...@arscreat.com>.
+1 G-) mg
On 11/12/2024 02:32, Paul King wrote:
> I created this pull request which clarifies things somewhat:
>
> https://github.com/apache/groovy/pull/2133
>
> But feel free to create an alternative if you think it can be improved further.
>
> Cheers, Paul.
>
> On Wed, Dec 11, 2024 at 12:58 AM Milles, Eric (TR Technology)
> <er...@thomsonreuters.com> wrote:
>>> 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: ocs@ocs.cz <oc...@ocs.cz>
>> Sent: Tuesday, December 10, 2024 8:49 AM
>> To: Groovy_Developers <de...@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....@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
>>
>>
>>
>>
Re: Using `var` as method return type placeholder
Posted by Paul King <pa...@asert.com.au>.
I created this pull request which clarifies things somewhat:
https://github.com/apache/groovy/pull/2133
But feel free to create an alternative if you think it can be improved further.
Cheers, Paul.
On Wed, Dec 11, 2024 at 12:58 AM Milles, Eric (TR Technology)
<er...@thomsonreuters.com> wrote:
>
> > 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: ocs@ocs.cz <oc...@ocs.cz>
> Sent: Tuesday, December 10, 2024 8:49 AM
> To: Groovy_Developers <de...@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....@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
>
>
>
>