ASTT to convert positional to named?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

ASTT to convert positional to named?

OCsite
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

MG
Reply | Threaded
Open this post in threaded view
|

Re: ASTT to convert positional to named?

MG
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), 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


Reply | Threaded
Open this post in threaded view
|

Re: ASTT to convert positional to named?

OCsite
MG,

On 31. 5. 2020, at 6:37 PM, MG <[hidden email]> 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), 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



MG
Reply | Threaded
Open this post in threaded view
|

Re: ASTT to convert positional to named?

MG
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, [hidden email] wrote:
MG,

On 31. 5. 2020, at 6:37 PM, MG <[hidden email]> 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), 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




Reply | Threaded
Open this post in threaded view
|

Re: ASTT to convert positional to named?

Mikko Värri


> On 1. Jun 2020, at 2.46, MG <[hidden email]> wrote:
>
> @MapConstructor
>
> On 31/05/2020 20:22, [hidden email] wrote:
>>
>>
>>> On 31. 5. 2020, at 6:37 PM, MG <[hidden email]> 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

MG
Reply | Threaded
Open this post in threaded view
|

Re: ASTT to convert positional to named?

MG
  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 [hidden email] wrote:

@MapConstructor

On 31/05/2020 20:22, [hidden email] wrote:

On 31. 5. 2020, at 6:37 PM, MG [hidden email] 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