Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10259

Groovy4 NV Macros: Rename NV* -> SV*

    XMLWordPrintableJSON

Details

    • Task
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.0.0-beta-1
    • 4.0.0-rc-2
    • None
    • None

    Description

      According to the release notes (https://groovy-lang.org/releasenotes/groovy-4.0.html) "stringify" macros will be released with Groovy 4, using a NV* naming convention.

      I propose to change the "stringify variable" macro names from
      NV/NVI/NVD
      to
      SV/SVI/SVD

      Rationale:

      • The planned Groovy4 NV macros immediately (G)Stringify their arguments, always creating the same fixed textual representation (which cannot be processed further in any meaningful way).
      • NV is the macro name I originally proposed (and currently use), but it returns a NameAndValue instance (see below for a slightly simplified version of my in any case trivial NameAndValue class), instead of directly returning a String representation
        • The NV macro name was chosen since it mirrors the name of the class it creates, which holds the stringified expression ("name") that has been passed to the NV macro, together with a reference to the result ("value") of evaluating said expression, i.e. in the end holds a name together with a value.
        • Note: A 2nd macro, NVL, in my implementation takes multiple arguments and creates a list of NameAndValue objects, i.e. NVL(x0,x1,x2) === [NV(x0),NV(x1),NV(x3)]
          • NV was kept to take only one argument, so additional arguments regarding output formatting could be supported in the future.
      • The returned NameAndValue objects can then be used flexibly to process further to e.g. create regex expressions, XML, test output, etc (or log/print directly if you like).
      • Both approaches by default create the same output when converted into a string, namely
        NV(x) -> "x=$x"
      • The current Groovy4 variety trades flexibility for performance (no intermediate NameAndValue objects are created), which is a good thing to have.
      • However it blocks both concepts from coexisting while using a concise, 2 letter macro name.

      I therefore propose to change the naming convention of the Groovy 4 macros from NV* to SV*, since it

      1. better expresses what happens ("stringify variable"), and
      2. allows for the name & value pairs of the (more flexible/powerful) NV* macro and NameAndValue class to coexist with it.

      Basic NameAndValue class:

      class NameAndValue<T> {
      	final String name
      	final T val
      
      	NameAndValue(String name, val) {
      	 this.name = name
      	 this.val = val
      	}
      
      	@Override
      	String toString() { 
      	   "$name=$val"
      	}
      } 
      

      NameAndValue class I actually use (with quote functionality):

      class NameAndValue<T> {
      	final String name
      	final T val
      
      	static <T0> NameAndValue<T0> nava(String name, T0 val) {
      		new NameAndValue<T0>(name, val)
      	}
      
      	NameAndValue(String name, val) {
      		this.name = name
      		this.val = val
      	}
      
      	String quote(final Object o) {
      		if(null == o) {
      			return null
      		}
      		else if(o instanceof GString) {
      			return '"' + o + '"'
      		}
      		else if(o instanceof String) {
      			return "'" + o + "'"
      		}
      		else if(o instanceof Character) {
      			return "C'" + o + "'"
      		}
      		else {
      			try {
      				return o.toString()
      			}
      			catch(Throwable throwable0) {
      				try {
      					return "NV(${o.getClass().simpleName}.toString() threw ${throwable0.getClass().simpleName})"
      				}
      				catch(Throwable throwable1) {
      					return "NV(o.getClass() threw ${throwable1.getClass().simpleName})"
      				}
      			}
      		}
      	}
      
      	@Override
      	String toString() {
      	   "$name=${quote(val)}"
      	}
      }
      

      Attachments

        Activity

          People

            paulk Paul King
            emge mgroovy
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 3.5h
                3.5h