You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by OCsite <oc...@ocs.cz> on 2020/05/31 10:45:01 UTC

ASTT to convert positional to named?

Hi there,

for both convenience and considerably improved source readability and robustness, I am considering an ASTT which would convert positional arguments to named ones, i.e., something like

@Named foo(bar,int bax=666,List baz) {
  ... whatever ...
}

turned to something like

foo(Map __map=null) {
  def bar=__map?.bar
  int bax=__map?.bax==null?666:__map?.bax
  List baz=__map?.baz
  ... whatever ...
}

In farther future with my own typechecking script it might even allow to typecheck named arguments, though that might prove a bit at the difficult side — but technically possible.

Has anybody tried something like that before? If so, bumped into some hidden pitfalls perhaps?

Thanks,
OC


Re: ASTT to convert positional to named?

Posted by MG <mg...@arscreat.com>.
 1. I don't seem to have gotten my own reply from the mailing list (also
    not in Junk/SPAM) - strange...
 2. Yes, treating null as nil would alas prohibit any usage of null as a
    parameter value. That is a long standing problem in many
    languages... - I wish there were support for more NULL-like
    constants (de facto there is room for at least 4, using memory
    adress values 1 to 3 in addition to 0 = NULL), such as NIL or EMPTY,
    to be able to distinguish these cases.
 3. I agree with the issue, it would be great if further improved named
    parameter support would be high on the agenda :-)

Cheers,
mg



On 01/06/2020 12:55, Mikko Värri wrote:
>
>> On 1. Jun 2020, at 2.46, MG <mg...@arscreat.com> wrote:
>>
>> @MapConstructor
>>
>> On 31/05/2020 20:22, ocs@ocs.cz wrote:
>>>
>>>> On 31. 5. 2020, at 6:37 PM, MG <mg...@arscreat.com> wrote:
>>>> @NamedVariant
>>> Thanks, will test the thing, probably it's precisely what I wanted to do.
>>>
>>>> On 31/05/2020 12:45, OCsite wrote:
>>>>>
>>>>> @Named foo(bar,int bax=666,List baz) {
>>>>>    ... whatever ...
>>>>> }
>>>>>
>>>>> turned to something like
>>>>>
>>>>> foo(Map __map=null) {
>>>>>    def bar=__map?.bar
>>>>>    int bax=__map?.bax==null?666:__map?.bax
>>>>>    List baz=__map?.baz
>>>>>    ... whatever ...
>>>>> }
>>>>>
> One thing it doesn't cover, afaik, is default values for the args.  But I don't think a simple null check will do: you can't pass "bar: null" to the method, then.
>
> There's at least one issue open for this: https://issues.apache.org/jira/browse/GROOVY-9532
>
> -mikko
>


Re: ASTT to convert positional to named?

Posted by Mikko Värri <mi...@varri.fi>.

> On 1. Jun 2020, at 2.46, MG <mg...@arscreat.com> wrote:
> 
> @MapConstructor
> 
> On 31/05/2020 20:22, ocs@ocs.cz wrote:
>> 
>> 
>>> On 31. 5. 2020, at 6:37 PM, MG <mg...@arscreat.com> wrote:
>>> @NamedVariant 
>> 
>> Thanks, will test the thing, probably it's precisely what I wanted to do.
>> 
>>> On 31/05/2020 12:45, OCsite wrote:
>>>> 
>>>> 
>>>> @Named foo(bar,int bax=666,List baz) {
>>>>   ... whatever ...
>>>> }
>>>> 
>>>> turned to something like
>>>> 
>>>> foo(Map __map=null) {
>>>>   def bar=__map?.bar
>>>>   int bax=__map?.bax==null?666:__map?.bax
>>>>   List baz=__map?.baz
>>>>   ... whatever ...
>>>> }
>>>> 

One thing it doesn't cover, afaik, is default values for the args.  But I don't think a simple null check will do: you can't pass "bar: null" to the method, then.

There's at least one issue open for this: https://issues.apache.org/jira/browse/GROOVY-9532

-mikko


Re: ASTT to convert positional to named?

Posted by MG <mg...@arscreat.com>.
Np, with the number of annotations in Groovy it's easy to overlook one - 
see if this one (or e.g. @MapConstructor 
http://docs.groovy-lang.org/2.5.0/html/gapi/index.html?groovy/transform/MapConstructor.html) 
does everything that you had in mind...
Cheers,
mg

On 31/05/2020 20:22, ocs@ocs.cz wrote:
> MG,
>
>> On 31. 5. 2020, at 6:37 PM, MG <mgbiz@arscreat.com 
>> <ma...@arscreat.com>> wrote:
>> Inhowfar would that differ from the existing map based argument 
>> support, like @NamedVariant etc 
>> (http://docs.groovy-lang.org/2.5.0/html/gapi/groovy/transform/NamedVariant.html) 
>> <http://docs.groovy-lang.org/2.5.0/html/gapi/groovy/transform/NamedVariant.html>, 
>> respectively what is your intention here ?
>
> Ha, I must be completely blind and dumb, too — I did check, but 
> somehow, I completely overlooked that ASTT!
>
> Thanks, will test the thing, probably it's precisely what I wanted to do.
>
> All the best,
> OC
>
>> On 31/05/2020 12:45, OCsite wrote:
>>> Hi there,
>>>
>>> for both convenience and considerably improved source readability 
>>> and robustness, I am considering an ASTT which would convert 
>>> positional arguments to named ones, i.e., something like
>>>
>>> @Named foo(bar,int bax=666,List baz) {
>>>   ... whatever ...
>>> }
>>>
>>> turned to something like
>>>
>>> foo(Map __map=null) {
>>>   def bar=__map?.bar
>>>   int bax=__map?.bax==null?666:__map?.bax
>>>   List baz=__map?.baz
>>>   ... whatever ...
>>> }
>>>
>>> In farther future with my own typechecking script it might even 
>>> allow to typecheck named arguments, though that might prove a bit at 
>>> the difficult side — but technically possible.
>>>
>>> Has anybody tried something like that before? If so, bumped into 
>>> some hidden pitfalls perhaps?
>>>
>>> Thanks,
>>> OC
>>>
>>
>


Re: ASTT to convert positional to named?

Posted by "ocs@ocs.cz" <oc...@ocs.cz>.
MG,

> On 31. 5. 2020, at 6:37 PM, MG <mg...@arscreat.com> wrote:
> Inhowfar would that differ from the existing map based argument support, like @NamedVariant etc (http://docs.groovy-lang.org/2.5.0/html/gapi/groovy/transform/NamedVariant.html) <http://docs.groovy-lang.org/2.5.0/html/gapi/groovy/transform/NamedVariant.html>, respectively what is your intention here ?

Ha, I must be completely blind and dumb, too — I did check, but somehow, I completely overlooked that ASTT!

Thanks, will test the thing, probably it's precisely what I wanted to do.

All the best,
OC

> On 31/05/2020 12:45, OCsite wrote:
>> Hi there,
>> 
>> for both convenience and considerably improved source readability and robustness, I am considering an ASTT which would convert positional arguments to named ones, i.e., something like
>> 
>> @Named foo(bar,int bax=666,List baz) {
>>   ... whatever ...
>> }
>> 
>> turned to something like
>> 
>> foo(Map __map=null) {
>>   def bar=__map?.bar
>>   int bax=__map?.bax==null?666:__map?.bax
>>   List baz=__map?.baz
>>   ... whatever ...
>> }
>> 
>> In farther future with my own typechecking script it might even allow to typecheck named arguments, though that might prove a bit at the difficult side — but technically possible.
>> 
>> Has anybody tried something like that before? If so, bumped into some hidden pitfalls perhaps?
>> 
>> Thanks,
>> OC
>> 
> 


Re: ASTT to convert positional to named?

Posted by MG <mg...@arscreat.com>.
Inhowfar would that differ from the existing map based argument support, 
like @NamedVariant etc 
(http://docs.groovy-lang.org/2.5.0/html/gapi/groovy/transform/NamedVariant.html) 
<http://docs.groovy-lang.org/2.5.0/html/gapi/groovy/transform/NamedVariant.html>, 
respectively what is your intention here ?
Cheers,
mg

On 31/05/2020 12:45, OCsite wrote:
> Hi there,
>
> for both convenience and considerably improved source readability and 
> robustness, I am considering an ASTT which would convert positional 
> arguments to named ones, i.e., something like
>
> @Named foo(bar,int bax=666,List baz) {
>   ... whatever ...
> }
>
> turned to something like
>
> foo(Map __map=null) {
>   def bar=__map?.bar
>   int bax=__map?.bax==null?666:__map?.bax
>   List baz=__map?.baz
>   ... whatever ...
> }
>
> In farther future with my own typechecking script it might even allow 
> to typecheck named arguments, though that might prove a bit at the 
> difficult side — but technically possible.
>
> Has anybody tried something like that before? If so, bumped into some 
> hidden pitfalls perhaps?
>
> Thanks,
> OC
>