Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy map syntax error

If I have this code :


import javax.swing.*
import java.awt.image.*

def xMap = [
    BufferedImage.TYPE_3BYTE_BGR     : "3 byte BGR",
    BufferedImage.TYPE_4BYTE_ABGR    : "4 byte ABGR",
]

the IDE will generate this error :

illegal colon after argument expression;
   solution: a complex label expression before a colon must be parenthesized at

Is there another solution to this than to write :


def type_3byte_bgr = BufferedImage.TYPE_3BYTE_BGR

for all the constants?

like image 679
Geo Avatar asked Jul 28 '09 18:07

Geo


2 Answers

Map literals require their keys to be valid identifiers or in parentheses. This should work:

def xMap = [
    (BufferedImage.TYPE_3BYTE_BGR)     : "3 byte BGR",
    (BufferedImage.TYPE_4BYTE_ABGR)    : "4 byte ABGR",]
like image 89
Andrew Duffy Avatar answered Nov 20 '22 11:11

Andrew Duffy


The error message tells you how to solve it: parenthesize the label expression.

import java.awt.image.BufferedImage

def xMap = [
    (BufferedImage.TYPE_3BYTE_BGR)     : "3 byte BGR",
    (BufferedImage.TYPE_4BYTE_ABGR)    : "4 byte ABGR",
]

println xMap[BufferedImage.TYPE_3BYTE_BGR]
like image 34
Steve Losh Avatar answered Nov 20 '22 11:11

Steve Losh