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

Alternative to "with" called "tap" that has an implicate "return delegate"

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.7.0
    • 2.5.0-alpha-1
    • syntax
    • None

    Description

      In this code:

      class Foo { String bar }
      def x = new Foo().with {
        bar = "baz"
      }
      

      "x" is now a String with the value "baz" thanks to the implied return. To work around this, we have to say:

      def x = new Foo().with {
        bar = "baz"
        return delegate
      }
      

      ...which is pretty ugly, to say the least.

      So can we modify the "with" syntax to have an implied "return delegate"? I doubt anyone was depending on the previous behavior for code correctness.

      Test will be this:

      class Foo { String bar }
      def x = new Foo().with {
        bar = "baz"
      }
      assert x instanceof Foo
      assert x.bar == "baz"
      

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              robertfischer Robert Fischer
              Votes:
              2 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: