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

Provide a consistent notation between method calls and index parameters with maps as index or param



    • Improvement
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.6.4, 1.7-beta-1
    • None
    • Compiler, parser-antlr2
    • None


      We can pass "named arguments" in method calls, making the following equivalent:

      foo.method( [a: 1, b: 2] )
      foo.method( a: 1, b: 2 ) // square brackets omitted

      We should allow the same notations for indices in the subscript notation:

      someObject[ [a: 1, b: 2] ]
      someObject[ a: 1, b: 2 ]

      Currently, it's not possible to use map entries or maps as indices for the subscript notation.
      So the idea of this JIRA improvement is to allow this.
      And people would be able to use a getAt(Map) method on their objects to have access to that notation.

      Some further remarks:

      • with method calls, it's even possible to use the foo.method a: 1, b: 2 notation, but of course, this is not possible with the subscript notation, as we need the square brackets
      • an example of using that map as index idea is for example to represent text pointers or text selections in some document handling application:
        def bookmark = someText[ 10 : 20 ] // a bookmark on line 10 column 20, or perhaps even chapter 10 paragraph 20
        def selection = someText [ 10:20, 11:5 ] // a text selection between line 10 column 20 and line 11 column 5
      • the sole drawback with map entries as indices is when we want to use variables for line numbers, as we have to use parens around the "key" so as not to have a textual key:
        def (line, column) = [10, 20]
        def bookmark = someText [ (line): column ]




            Unassigned Unassigned
            guillaume Guillaume Sauthier
            0 Vote for this issue
            0 Start watching this issue