Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To explain this output, we need to understand additional rules about GStrings. Each value in the GString (where $ appears) is the result of an expression. Most often, it's an expression that results the expression is evaluated resulting in an Object, as in all the examples above except for the case of the variable y in which the expression is a Closure.

...

  • General Object instances are converted by the toString() method; there is special handling for types like array, Stream, Collection, etc.
  • Closure instances taking 0 parameters are called and the returned value is converted to String by the same rules as for a general Object (a returned Closure will not be called).
  • Closure instances taking 1 parameter are called with a Writer as the parameter; characters written to the Writer become part of the String result.   The return from the Closure is ignored.
  • Closure instances taking more than 1 parameter are not called; the GString throws GroovyRuntimeException.

...