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

Eager interpolation evaluating as a Closure does a lazy invoke of the Closure

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.5.5
    • None
    • None
    • Windows 10

    Description

      This is regarding the eager interpolation placeholder

      ${ <expression> }

      The expected behavior is that the expression would be evaluated eagerly (at GString creation time). Later, at GString coercion time, the value that is expected in the place of the eager placeholder is the result of

      <expression>.toString()

       

       

      This works most of the time, but doesn't seem to work if the expression evaluates to a Closure type. If the expression evaluates to a Closure type, what is observed is that, in the place of the placeholder, instead of seeing ...

      <closure>.toString()

      ... we see...

      <closure-invocation-result>.toString()

      For example, in the below code...

       

      def c = {->println ('Inside closure')}
      println ('Just before creating the GString')
      def gstr = "${println ('Inside eager placeholder'); 20; 30; c}"
      println ('Just after creating the GString')
      println (gstr)
      

       

      ...the expected behavior is that the closure should never get executed, and the last println should only print the result of

      c.toString()

      Instead, the last line printed is observed to be:

      Inside closure

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            iamchaitanya Chaitanya Birudavolu
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated: