Search code examples
dictionaryarraylistgroovytypesinference

Tell Groovy Type of Object in Map using Bracket Notation


I'm always battling eclipse underlining properties or methods called upon objects I reference using bracket notation. Here's a simple example. Looking for most efficient and succinct way to tell compiler that this is a file, and therefor .write is valid, and so don't underline it.

For novice readers, I'm making a map on the fly to add the file and it's contents to the arraylist.

def myOutputList = new ArrayList()
def myFileObject = new File("outputFile.txt")
def myFileContents = "Whargarble"

myOutputList.add([
    file:myFileObject,
    contents:myFileContents
])

myOutputList.each{
    it['file'].write(it['contents'])
}

Solution

  • You can state the type explicitly - then Eclipse will cooperate.

    In your example:

    File f = it['file']
    f.write...
    

    You can also use a class instead of a map (again, with explicit types)