groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul King <>
Subject Re: Groovy Name-and-Value Macro Support Proposal
Date Thu, 28 Jun 2018 07:23:52 GMT
Looks nice.

I'll have to think about conventions on where such contributions should
live and naming conventions too.

We already have "macro" which is lowercase. Sometimes I suspect we want to
make it obvious that we are dealing with a macro, other times we want to
almost hide that fact. I am not sure one single convention is going to work.

My current inclination is to delay making any final decisions on such
topics until we see what else emerges. Though I must admit I like your
current proposal as is.

Cheers, Paul.

On Thu, Jun 28, 2018 at 5:10 AM MG <> wrote:

> Hi,
> I have given Groovy 2.5's new macro support a spin and implemented
> functionality to support code constructs which I could last use with C++'s
> macro preprocessor stringify operation (and which I always found very
> helpful for debugging / creating runtime log output).
> Example:
> final ageOfTree = 124final towerHeight = 987.654final String visitorName = "abc"final
s1 = "DEFGH"final gs0 = "val0:$ageOfTree"final GString gs1 = "TheTower($towerHeight)"final
List names = [ "Peter", "Ann", "Raymond" ]
> println "single variables: ${NV(ageOfTree)} and ${NV(gs0)} and ${NV(visitorName)} and
${NV(s1)} and ${NV(gs1)} and also ${NV(names)})}"println "variable list: ${NVL(ageOfTree,
gs0, visitorName, s1, towerHeight, gs1, names)}"
> which prints:
> single variables: ageOfTree=124 and gs0="val0:124" and visitorName="abc" and s1="DEFGH"
and gs1="TheTower(987.654)" and also names=[Peter, Ann, Raymond])}variable list: [ageOfTree=124,
gs0="val0:124", visitorName="abc", s1="DEFGH", towerHeight=987.654, gs1="TheTower(987.654)",
names=[Peter, Ann, Raymond]]
> NV here is a Groovy macro method which, which allows turning the passed variable expression
into its name and value in the form of a simple NameAndValue class instance. NVL does the
same for multiple variables (returning a list of NameAndValue|s).
> (I used a different name for NVL, to allow for NV to take more than one variable expression
parameter (and return e.g. a GString that contains a sequence of "name=$val" expressions without
the enclosing list brackets), or to allow for NV to take additional parameters).
> Such an extension would be useful, because it:
>    1. Reduces the amount of boilerplate code one needs to write
>    2. Makes the code more concise
>    3. Removes errors such as: println "var0=$var0, var1=$var0"
> I therefore propose we add this functionality to Groovy 2.5.x .
> Apart from the general decision, open questions for me are:
>    1. Where do we put macro extensions in the Groovy source code ?
>    2. What names should we use for macro methods ?
> I lean towards using C/C++ macro naming conventions as a guideline, i.e.
> using short, abbreviated, uppercase-only names such as NV = named variable,
> or NVL = named variable list, since:
>    1. Macros allow for "magic" and are not really method calls in the
>    classical sense
>    2. Java do not have macros, so the hope would be that making them
>    stick out a bit will make them less confusing for developers coming from
>    Java to Groovy
>    3. On the other hand C/C++ developers that are used to macros will
>    immediately suspect that they are looking at a macro-like construct
>    4. Independent of upper-/lowercase, brevity is important, especially
>    for the proposed two NV/NVL macros, since it would imho be desirable for
>    e.g. "NV(x)" to not take up more space than "x=$x"
> In any case macro-method support in Groovy is a fantastic feature, which
> opens up completely new possibilities for language & framework developers -
> Go Groovy !-)
> Please comment,
> Cheers,
> mg

View raw message